jquery radio checkbox操作集合
select 操作:
遍历option取值:
$("#select_left").children("option").each(function() {
console.log(this.value);
});
遍历选中:
$("#select_left").children("option").each(function() {
this.selected = true;
jQuery(this).attr("selected", false).appendTo($("#select_right"));
});
、、、、、、、、、、、、、、、、、、、、、、
、、、、、、、、、、、、、、、、、、、、、
语法解释:
1. $("input[name='radio_name'][checked]").val(); //选择被选中Radio的Value值
2. $("#text_id").focus(function(){//code...}); //事件 当对象text_id获取焦点时触发
3. $("#text_id").blur(function(){//code...}); //事件 当对象text_id失去焦点时触发
4. $("#text_id").select(); //使文本框的Vlaue值成选中状态
5. $("input[name='radio_name'][value='要选中Radio的Value值']").attr("checked",true); //根据Value值设置Radio为选中状态
语法解释:
1. $("input[name='checkbox_name'][checked]"); //选择被选中CheckBox元素的集合 如果你想得到Value值,你需要遍历这个集合
2. $($("input[name='checkbox_name'][checked]")).each(function(){arrChk+=this.value + ',';}); //遍历被选中CheckBox元素的集合 得到Value值
3. $("#checkbox_id").attr("checked"); //获取一个CheckBox的状态(有没有被选中,返回true/false)
4. $("#checkbox_id").attr("checked",true); //设置一个CheckBox的状态为选中(checked=true)
5. $("#checkbox_id").attr("checked",false); //设置一个CheckBox的状态为不选中(checked=false)
6. $("input[name='checkbox_name']").attr("checked",$("#checkbox_id").attr("checked")); //根据3,4,5条,你可以分析分析这句代码的意思
7. $("#text_id").val().split(","); //将Text的Value值以','分隔 返回一个数组
、、、、、、、、、、、、、、、、、、、、、、、、
、、、、、、、、、、、、、、、、、、、、、、、、、
纠正jQuery获取radio选中值的写法
<input type="radio" name="aaa" value="1" checked="true">aaa
<input type="radio" name="aaa" value="2">bbb
<input type="radio" name="aaa" value="3">ccc
<input type="button" value="ok" onclick="doTest()">
<script language="JavaScript"> <!-- function doTest(){ alert($("input[name='aaa'][checked]").val()); } //--> </script>
网上流行的说法就是
$(input[name='aaa'][checked]).val()
能取到选中项的value,但我测试后发现只在IE下有效,在firefox和Chrome中不论选中哪一项,或者不选,取到的值都是第一项的value
正确做法应该是
$("input[name='aaa']:checked").val()
同样对于checkbox也是这种写法

浙公网安备 33010602011771号