Jquery 动态增加option及获取值 遍历option相关方法
来源:互联网 发布:成捷迅线路软件2004 编辑:程序博客网 时间:2024/05/17 16:56
data里的数据如下图:
html:
<select id="channel_id" name="channel"></select>
js:
function getBaseOptionFun(){$('#channel_id').empty();$.ajax({ url: "../../ueIndexConfig/getOption.do", type: "POST", data: { province_id:'', channel_id:'' }, async : false, success: function (data) { console.log(data); if(data.all_channel != "" && data.all_channel != null){for(var i = 0; i < data.all_channel.length; i++){$("#channel_id").append("<option value='"+data.all_channel[i].channel_id+"'>"+data.all_channel[i].channel_name+"</option>");//新增}$("#channel_id option:eq(0)").attr('selected', 'selected');//选中第一个//$("#channel_id").append("<option value=''>请选择</option>");} }, fail: function (status) { // 此处放失败后执行的代码 } }); }
获取select 中选中的值
方法:
//获取select中值$("#channel_id option:selected").val();//获取select中值channel_id$("#channel_id").val();//获取select中文本channel_name $("#channel_id").text(); //$("#channel_id").find("option:selected").text();//获取select 中下标值$("#channel_id").get(0).selectedIndex;//获取select 最大的index属性值$("#channel_id option:last").attr("index");
//select选中索引有好多方式,$('#someId').find('option:selected').selectedIndex;$('#someId').find('option:selected').attr('selectedIndex');这两种方式取不到索引值$('#someId').prop('selectedIndex');$('option:selected', '#someId').index();$('#someId option').index($('#someId option:selected'))//以上三种方式可以取到索引值
遍历option获取所有值
html:
<select id="channel_id" name="channel" datatype="*" nullmsg="请选择渠道"><option value="1" selected="selected">掌上营业厅</option><option value="2">网上营业厅</option><option value="3">微信营业厅</option><option value="8">能力开放平台</option></select>
js:
//方法1:$(function(){ var array = new Array(); //定义数组 $("#channel_id option").each(function(){ //遍历所有option var channlVal= $(this).val(); //获取option值 if(channlVal!=''){ array.push(channlVal); //添加到数组中 } }) }) ; //方法2:var channelArr= new Array();var channel=$("#channel_id").find("option");for(var i=0;i<channel.length;i++){var channlVar=channl.eq(i).val();//option中的值channelArr.push(channlVar);//添加到数组中}
jQuery添加/删除Select的Option项:1. $("#select_id").append("<option value='Value'>Text</option>"); //为Select追加一个Option(下拉项)2. $("#select_id").prepend("<option value='0'>请选择</option>"); //为Select插入一个Option(第一个位置)3. $("#select_id option:last").remove(); //删除Select中索引值最大Option(最后一个)4. $("#select_id option[index='0']").remove(); //删除Select中索引值为0的Option(第一个)5. $("#select_id option[value='3']").remove(); //删除Select中Value='3'的Option5. $("#select_id option[text='4']").remove(); //删除Select中Text='4'的Option
阅读全文
0 0
- Jquery 动态增加option及获取值 遍历option相关方法
- jquery动态添加以及遍历option并获取特定样式名称的option
- jquery动态添加以及遍历option并获取特定样式名称的option
- jQuery获取option方法汇总
- jQuery获取option方法汇总
- 使用jquery增加option
- jquery动态获取select控件的option
- <select><option>值获取方法
- jQuery——获取option方法汇总
- jQuery获取option
- jQuery获取option
- jQuery获取select中所有option值
- Jquery attr获取option属性值
- jquery获取select选项option的值
- jQuery获取select中所有option值
- 关于js遍历list集合,获取select选中的值以及动态添加option
- JS & JQuery 动态添加 option
- jquery select option 动态选中
- 关于淘客软件的那些事儿
- windows下nginx的安装及使用
- 运行hadoop运行示例的几种方法
- 神经网络多轮对话系统(续)
- LINTCODE——将数组重新排序以构造最小值
- Jquery 动态增加option及获取值 遍历option相关方法
- c++ 获取当前时间,当前日期
- 用QRCode.js制作二维码的生成器(qrcode.makeCode方法生成二维码)
- Hive 之 关于时间的函数
- JDBC不同驱动程序名称与URL格式
- mybatis中"#"和"$"的区别
- 生成判别对抗网络
- java原生命令
- 《分布式服务架构原理设计与实战》第2章分布式系统一致性的问题笔记