AJAX 是一种用于创建快速动态网页的技术。

通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步更新。这意味着可以在不重新加载整个网页的情况下,对网页的某部分进行更新,用户体验非常好。

下面介绍两种动态加载DropDownList值的方法。

第一种:

View层

<select id="funcNum" name="funcNum"></select>
<script>
$(document).ready(function() {
showFuncId();
}
function showFuncId(){
$('#funcNum').empty();
var ciValue = $('#funcNum');
ciValue.append('<option value="">Pls Select</option>');
$.ajax({
url : u, //your actual url
type : 'post',
dataType : 'json',
success : function (opts) {
if (opts && opts.length > 0) {
var html = [];
for (var i = 0; i < opts.length; i++) {
html.push('<option value="'+opts[i].id+'">'+opts[i].desc+'</option>');
}
ciValue.append(html.join(''));
}
}
});
}
</script> 

Controller层

      response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
PrintWriter writer = response.getWriter();
net.sf.json.JSONArray array = JSONArray.fromObject(new ArrayList());//The list that contains actual data,use a new arrayList instead here
writer.append(array.toString());
return null;  

第二种:

View层

<select id="funcNum" name="funcNum"></select>
<script>
$(function(){
$.ajax({
type: 'POST',
url:url;//your actual url
dataType: 'json',
cache: false,
async:false,
success:function(data) {
$('#funcNum').get(0).options.length = 0;
$('#funcNum').append('<option value="">Pls Select</option>');
$.each(data, function(i, obj) {
var option = $('<option />');
option.val(obj.id);
option.text(obj.desc);
$('#funcNum').append(option);
});
},
error: function() {
alert("Error while getting func num results");
}
});
});
</script>

Controller层

             response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
PrintWriter writer = response.getWriter();
net.sf.json.JSONArray array = JSONArray.fromObject(new ArrayList());//The list that contains actual data,use a new arrayList instead here
writer.append(array.toString());
return null;

  

 Note:

As JSON format supports the following data types . 

1

Number

double- precision floating-point format in JavaScript

2

String

double-quoted Unicode with backslash escaping

3

Boolean

true or false

4

Array

an ordered sequence of values

5

Value

it can be a string, a number, true or false, null etc

6

Object

an unordered collection of key:value pairs

7

Whitespace

can be used between any pair of tokens

8

null

empty

  for date value, need add config to fromObject, core code:

  

 JsonConfig config = new JsonConfig();
config.registerJsonBeanProcessor(java.sql.Date.class, new JsDateJsonBeanProcessor());
JSONArray array = JSONArray.fromObject(object, config);

  

  

使用jquery的ajax方法获取下拉列表值的更多相关文章

  1. jquery通过ajax方法获取json数据不执行success

    1.jquery通过ajax方法获取json数据不执行success回调 问题描述:jquery通过ajax方法获取json数据不执行success回调方法 问题原因:json格式存在问题或不符合标准 ...

  2. jquery通过ajax方法获取json数据不执行success回调

    问题描述:jquery通过ajax方法获取json数据不执行success回调方法 问题原因:json格式存在问题或不符合标准写法,导致总是执行error回调方法 解决方案:使json格式务必符合下述 ...

  3. 用jquery的ajax方法获取return返回值的正确姿势

    如果jquery中,想要获取ajax的return返回值,必须注意两方面,ajax的同步异步问题,在ajax方法里面还是外面进行return返回值. 下面列举了三种写法,如果想成功获取到返回值,参考第 ...

  4. 用jquery的ajax方法获取不到return返回值

    如果jquery中,获取不到ajax返回值. 两个错误写法会导致这种情况:1.ajax未用同步 2.在ajax方法中直接return返回值. 下面列举了三种写法,如果想成功获取到返回值,参考第三种写法 ...

  5. JQuery的ajax方法获取返回值得到了值还包含了不要的html源码 (Ajax相关知识)

    因为后台使用了response.Write():这个方法,当输出完了以后,没有结束掉会继续输出,所以需要用到response.End():这样问题就解决了 jquery的ajax 除了通过url传值, ...

  6. 用JQuery中的Ajax方法获取web service等后台程序中的方法

    用JQuery中的Ajax方法获取web service等后台程序中的方法 1.准备需要被前台html页面调用的web Service,这里我们就用ws来代替了,代码如下: using System; ...

  7. 使用Jquery解决Asp.Net中下拉列表值改变后访问服务器刷新界面。

    使用DropDownList控件时,改变选项时,获取服务端数据库数据并刷新界面数据. 1. 绑定DropDownList控件SelectedIndexChanged事件. 2. AutoPortBac ...

  8. 关于Jquery中ajax方法data参数用法的总结

    data 发送到服务器的数据.将自动转换为请求字符串格式.GET 请求中将附加在 URL 后.查看 processData 选项说明以禁止此自动转换.必须为 Key/Value 格式.如果为数组,jQ ...

  9. ajax系列之用jQuery的ajax方法向服务器发出get和post请求

    打算写个ajax系列的博文,主要是写给自己看,学习下ajax的相关知识和用法,以更好的在工作中使用ajax. 假设有个网站A,它有一个简单的输入用户名的页面,界面上有两个输入框,第一个输入框包含在一个 ...

随机推荐

  1. jQuery 文档操作 - insertAfter() ,insertBefore(),after(),before() 方法

    这个方法跟prependTo()和appendTo()不一样的地方在于,一个是仍然插入到元素内部,而insertAfter和insertBefore是插入到元素外部. 这里拿insertBefore来 ...

  2. __block 和__weak 区别及使用

    API Reference对__block变量修饰符有如下几处解释: //A powerful feature of blocks is that they can modify variables ...

  3. Chap 2 Representing and Manipulating Information (CS:APP)

    -------------------------------------------------------------------------------------------------- A ...

  4. linq基础,正则表达式,.net相关

    声明扩展方法的步骤:类必须是static,方法是static,第一个参数是被扩展的对象,前面标注this.使用扩展方法的时候必须保证扩展方法类已经在当前代码中using.  LINQ:将int数组中大 ...

  5. c++ simple class template example: Stack

    main.cpp #include "Stack.h" #include <iostream> using namespace std; class Box { pub ...

  6. MFC 点击按钮,弹出另一个对话框(模态及非模态对话框)

    1. 模态对话框 资源视图->Dialog->右键->添加资源->新建->对话框->右键->添加类. 例如:在A_dialog中点击按钮弹出B_dialog  ...

  7. php方法重写:Declaration of should be compatible with that

    <?php// this code does trigger a strict messageerror_reporting( E_ALL | E_STRICT ); class cc exte ...

  8. sqrt函数实现(神奇的算法)

    我们平时经常会有一些数据运算的操作,需要调用sqrt,exp,abs等函数,那么时候你有没有想过:这个些函数系统是如何实现的?就拿最常用的sqrt函数来说吧,系统怎么来实现这个经常调用的函数呢? 虽然 ...

  9. E - Leading and Trailing 求n^k得前三位数字以及后三位数字,保证一定至少存在六位。

    /** 题目:E - Leading and Trailing 链接:https://vjudge.net/contest/154246#problem/E 题意:求n^k得前三位数字以及后三位数字, ...

  10. VLC Web插件的浏览器兼容性

    网页插件实现原理 IE浏览器基于Activex插件来实现,非IE浏览器采用NPAPI来实现,所以,非浏览器需要支持NPAPI来实现. IE浏览器 FF浏览器(版本小于52) 原因从 Firefox 版 ...