信息内容
您现在的位置:首页-信息内容
js和jq判断select是否选中、获取select选中的值 发布日期:2018-1-5 21:46:44 来源:csdn 【关闭】

js和jq获取select选中的值

JS部分

var   mySelect = document.getElementById(”testSelect”);//定位id(获取select)
var   index =mySelect.selectedIndex;// 选中索引(选取select中option选中的第几个)
var   text =mySelect.options[index].text; // 选中文本
var   value =mySelect.options[index].value; // 选中值

mySelect.options[index].selected // 判断select中的某个option是否选中   true为选中   false 为未选中


if(mySelect.options[1].selected == true){ 

   console.log(1)  

}  


JQ部分

1.判断option是否被选中

$("#id").is(":checked")//为false时是未被选中的,为true时是被选中

$("#id").attr('checked')==undefined//为false时是未被选中的,为true时是被选中

2.获取select选中的值

$("#mySelect option:selected").text()

$("#mySelect").find('option:selected').text()

$("#mySelect").val();

3.获取select选中的索引

$("#mySelect").get(0).selectedindex

4.添加option

$("#mySelect").append("<option value="+value+">"+text+"<option>");

5.删除option

$("#myOption").remove()



成都世帆软件    交通运输综合执法系统软件、公路路政执法系统软件、道路运政执法系统软件、海事行政、水路运政、航道行政、港口行政执法 管理系统软件定制开发

交通综合执法系统、公路路政执法管理系统、超限运输管理系统、道路运政执法管理系统、运管执法管理系统、公路养护管理系统、公路运行监测系统
市场监管执法软件、工商行政执法软件、环境监察执法软件、城市管理执法软件、食品卫生执法软件

备案号:蜀ICP备12010221号-20    电-话:028-8768 9944