使用jquery的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方法获取下拉列表值的更多相关文章
- jquery通过ajax方法获取json数据不执行success
1.jquery通过ajax方法获取json数据不执行success回调 问题描述:jquery通过ajax方法获取json数据不执行success回调方法 问题原因:json格式存在问题或不符合标准 ...
- jquery通过ajax方法获取json数据不执行success回调
问题描述:jquery通过ajax方法获取json数据不执行success回调方法 问题原因:json格式存在问题或不符合标准写法,导致总是执行error回调方法 解决方案:使json格式务必符合下述 ...
- 用jquery的ajax方法获取return返回值的正确姿势
如果jquery中,想要获取ajax的return返回值,必须注意两方面,ajax的同步异步问题,在ajax方法里面还是外面进行return返回值. 下面列举了三种写法,如果想成功获取到返回值,参考第 ...
- 用jquery的ajax方法获取不到return返回值
如果jquery中,获取不到ajax返回值. 两个错误写法会导致这种情况:1.ajax未用同步 2.在ajax方法中直接return返回值. 下面列举了三种写法,如果想成功获取到返回值,参考第三种写法 ...
- JQuery的ajax方法获取返回值得到了值还包含了不要的html源码 (Ajax相关知识)
因为后台使用了response.Write():这个方法,当输出完了以后,没有结束掉会继续输出,所以需要用到response.End():这样问题就解决了 jquery的ajax 除了通过url传值, ...
- 用JQuery中的Ajax方法获取web service等后台程序中的方法
用JQuery中的Ajax方法获取web service等后台程序中的方法 1.准备需要被前台html页面调用的web Service,这里我们就用ws来代替了,代码如下: using System; ...
- 使用Jquery解决Asp.Net中下拉列表值改变后访问服务器刷新界面。
使用DropDownList控件时,改变选项时,获取服务端数据库数据并刷新界面数据. 1. 绑定DropDownList控件SelectedIndexChanged事件. 2. AutoPortBac ...
- 关于Jquery中ajax方法data参数用法的总结
data 发送到服务器的数据.将自动转换为请求字符串格式.GET 请求中将附加在 URL 后.查看 processData 选项说明以禁止此自动转换.必须为 Key/Value 格式.如果为数组,jQ ...
- ajax系列之用jQuery的ajax方法向服务器发出get和post请求
打算写个ajax系列的博文,主要是写给自己看,学习下ajax的相关知识和用法,以更好的在工作中使用ajax. 假设有个网站A,它有一个简单的输入用户名的页面,界面上有两个输入框,第一个输入框包含在一个 ...
随机推荐
- RecycleView实现多布局可展开列表
代码地址如下:http://www.demodashi.com/demo/13193.html 前言 在开发的时候,我们不免会遇到这么一种数据展示,该数据有以下特征: 数据要以列表形式展示 每条数据要 ...
- oracle闪回某个时间段的数据
闪回2013-08-08 08:08:08的数据: insert into table_1 select * from table_2 as of timestamp to_date('2013 ...
- 使用CXF实现基于Soap协议的WebService
本文介绍使用CXF实现基于Soap协议的WebService(CXF的版本是3.0.0) 一. 前言 Java有三种WebService规范:Jax-WS,Jax-RS,Jaxm 1. Jax-WS( ...
- centos下nginx启动脚本和chkconfig管理
在安装完nginx后,重新启动需要“kill -HUP nginx进程编号”来进行重新加载,显然十分不方便.如果能像apache一样,直接通过脚本进行管理就方便多了. nginx官方早就想好了,也提供 ...
- View的setTag和getTag使用
在listview 优化其中,会使用到setTag()以及getTag()方法 代码例如以下: @Override public View getView(int position, View con ...
- Linux Chrome Tab 标题 乱码
1. 刚装完ubuntu 14.04 英文版, 又装了google chrome 浏览器. 2. 打开chrome浏览器,发现tab也没的标题是乱码: 3. 而系统自带的firefox却没有这个问题, ...
- JAVA 利用SimpleDateFormat将String转换为格式化的日期
1. /** * 使用用户格式提取字符串日期 * * @param strDate 日期字符串 * @param pattern 日期格式 * @return */ public static Dat ...
- python 第三方库下载地址
http://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml
- script 标签里的 async 和 defer
无 async 和 defer 浏览器立即加载并执行指定脚本(读到即加载并执行),阻塞文档解析 async 脚本的加载执行和文档的加载渲染 并行. defer 脚本的加载和文档的加载渲染并行,但脚本的 ...
- mock带参数的构造函数
@RunWith(PowerMockRunner.class)@PrepareForTest(Helper.class)//1.添加要初始化的类,就是构造函数所在的类public class Help ...