js/jq获取 select选中的值

1、JS
//获取选中的值
var sel=document.getElementById(“o_type”);
var index = sel.selectedIndex; //
o_type= sel.options[index].value;//支付方式

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

2、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(""+text+"");

5.删除option
$("#myOption").remove();

猜你喜欢

转载自blog.csdn.net/weixin_43356295/article/details/82999212