Jquery提供的选择器极大的方便了开发人员对Dom的操作,真正实现了代码简化,却功能强大的目标。下面就日常最常用的,在Form表单中如何获取被中选的Radio值做一小小的示例。
form表单如下:
<form name='form1' action="#" method="post">
此处略去200字
<input type="radio" name="opType" value="0" />搁置<br />
<input type="radio" name="opType" value="1" />解决<br />
<input type="radio" name="opType" value="2" />转派4<br />
</form>
那么如何获取被选中的radio值呢,Juqery为我们提供了如下几个方法
$("
input[name='opType']:checked"
).val() -------此方法估计用的比较多,通俗易懂
$("
input:radio:checked"
).val();
---------此方法最简单,但是连着使用选择器不容易懂
$("
input[@name='opType'][checked]"
);
--------次方法中切记写成[@checked=checked],本人第一次就写成这个了
![抓狂](http://static.blog.csdn.net/xheditor/xheditor_emot/default/crazy.gif)
那么,偶尔也需要遍历一下radio,如何做呢?当然需要each出场了,具体如下:
$('input[name="opType"]').each(function(){
alert(this.name+this.value);
});
应该明白了吧,若有什么更好的方法欢迎盖楼。![大笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/laugh.gif)
![大笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/laugh.gif)
![大笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/laugh.gif)
![大笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/laugh.gif)
![大笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/laugh.gif)
![大笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/laugh.gif)
![大笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/laugh.gif)
![大笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/laugh.gif)
![大笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/laugh.gif)
![大笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/laugh.gif)