使用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,它有一个简单的输入用户名的页面,界面上有两个输入框,第一个输入框包含在一个 ...
随机推荐
- 基于iOS 10、realm封装的下载器
代码地址如下:http://www.demodashi.com/demo/11653.html 概要 在决定自己封装一个下载器前,我本以为没有那么复杂,可在实际开发过程中困难重重,再加上iOS10和X ...
- TCP/IP ---互联网的地址
互联网上的每个接口必须有一个唯一的I n t e r n e t地址(也称作I P地址). I P地址长32 bit.I P地址具有一定的结构,五类不同 的互联网地址格式如图1 - 5所示. 这些3 ...
- STM32 通用T2、T3、T4、T5定时器详解
定时器初始化配置 void TIM3_Configuration(void)//1MS { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_DeI ...
- Windows 8 64bit Xilinx ISE(14.7) Fix License
http://www.youtube.com/watch?v=ttPbEcNjdo8 It can work successfully!
- hdu 2217 Visit
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2217 题目解释:起始位置在原点,给你固定的时间,让你左右跑,求在规定的时间内你最多能跑多少个点: 解决 ...
- Python、Lua和Ruby之优劣
Python.Lua和Ruby之优劣 Python Python的优点:1.Python比其它语言有更多扩展模块.2.在网上可以找到很多Python教程.不仅如此,还有大量的英文书籍和资料.Pytho ...
- Android项目使用Dagger2进行依赖注入
原文链接:http://code.tutsplus.com/tutorials/dependency-injection-with-dagger-2-on-android–cms-23345 依赖注入 ...
- Modification of UCT with Patterns in Monte-Carlo Go(论文阅读)
摘要:用于解决多臂赌博机UCB1算法已经被扩展成了解决极大极小树搜索的UCT算法.我们开发了一套Monte-Carlo围棋程序,MoGo,这是第一个使用UCT算法实现的计算机围棋程序.我们解释了为了围 ...
- gcc使用备忘
本文为原创文章,转载请指明该文链接 Options Controling the kind of Output -x language 明确说明输入文件的编码语言,没有该选项的话, gcc 会根据输入 ...
- js 简单的样式封装
<script> function test(value){ var str=value; document.write("<div style=\"width: ...