首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

jQuery动态增添删除select项

2013-09-05 
jQuery动态添加删除select项$(#mySelect).change(function(){//code...})//select选中项改变时触发//

jQuery动态添加删除select项

$("#mySelect").change(function(){//code...});    //select选中项改变时触发// 获取select值var text=$("#mySelect").find("option:selected").text();   //获取Select选中项的Textvar value=$("#mySelect").val();   //获取Select选中项的Valuevar value=$("#mySelect option:selected").attr("value");   //获取Select选中项的Valuevar index=$("#mySelect").get(0).selectedIndex;   //获取Select选中项的索引值,从0开始var index=$("#mySelect option:selected").attr("index");   //不可用!!!var index=$("#mySelect option:selected").index();   //获取Select选中项的索引值,从0开始var maxIndex=$("#mySelect option:last").attr("index");   //不可用!!!var maxIndex=$("#mySelect option:last").index();//获取Select最大索引值,从0开始$("#mySelect").prepend("<option value='value'>text</option>");   //Select第一项前插入一项// 设置select值//根据索引设置选中项$("#mySelect").get(0).selectedIndex=index;//index为索引值 //根据value设置选中项$("#mySelect").attr("value","newValue"); $("#mySelect").val("newValue"); $("#mySelect").get(0).value = value; //根据text设置对应的项为选中项var count=$("#mySelect option").length; for(var i=0;i<count;i++) {     if($("#mySelect").get(0).options[i].text == text)     {         $("#mySelect").get(0).options[i].selected = true;         break;     } } // 清空select$("#mySelect").empty();

热点排行