姚博文 JQuery 操作select取值,选中

本文介绍了一种使用jQuery操作Select元素的方法,确保了在不同浏览器(如Firefox)间的良好兼容性。文中通过实例展示了如何判断Select中Option元素的选中状态及如何设置特定Option为选中状态。

//var select0 = $("select[@name=select_templet"+ thissrc + "]")[0].options(0).selected;
//var select1 = $("select[@name=select_templet"+ thissrc + "]")[0].options(1).selected;
//var select2 = $("select[@name=select_templet"+ thissrc + "]")[0].options(2).selected;

  var select0 = $("#select_templet"+ thissrc + " option:eq(0)").attr("selected");
  var select1 = $("#select_templet"+ thissrc + " option:eq(1)").attr("selected");
  var select2 = $("#select_templet"+ thissrc + " option:eq(2)").attr("selected");

 

最近作项目用到jquery,网上许多操作select都用到上面我注释掉的代码,结果在firefox中根本不好使,下面三行是我使用jquery自己的代码后,完全兼容.上面代码实现了判断option某个元素是否为选中状态

 

//$("select[@name=select_templet"+ mateid + "]")[0].options(parseInt(type)).selected=true;

//$("select[@name=select_act"+ mateid + "]")[0].options(parseInt(act)).selected=true;

 $("#select_templet"+ mateid).attr("value",parseInt(type));
 $("#select_act"+ mateid).attr("value",parseInt(act))

 

以上的代码实现了对第index个option的选中

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值