Jquery操作radio,checkbox,select表单操

2013/10/02 1493点热度 0人点赞 0条评论

radio操作

<input type="radio" name="addsign" value="beforeSign" id="beforeSign">前加签
<input type="radio" name="addsign" value="aftersign" id="aftersign">后加签
<input type="radio" name="addsign" value="addsign" id="addsign" checked >并行加签
$('input[name="addsign"]:checked').val();
$('input:radio:checked').val();
$('input[@name="addsign"][checked]');
$('input[name="addsign"]').filter(':checked');
差不多挺全的了,如果我们要遍历name为testradio的所有radio呢,代码如下
$('input[name="addsign"]').each(function(){
    alert(this.value);
});

Select操作

jQuery 获取Select选择的Text和Value

 $("#select_id").change(function(){//code...}); //为Select添加事件,当选择其中一项时触发 
 var checkText=$("#select_id").find("option:selected").text(); //获取Select选择的Text 
 var checkValue=$("#select_id").val(); //获取Select选择的Value 
 var checkIndex=$("#select_id ").get(0).selectedIndex; //获取Select选择的索引值 
 var maxIndex=$("#select_id option:last").attr("index"); //获取Select最大的索引值

jQuery设置Select选择的Text和Value

 $("#select_id ").get(0).selectedIndex=1; //设置Select索引值为1的项选中 
 $("#select_id ").val(4); //设置Select的Value值为4的项选中 
 $("#select_id option[text='jQuery']").attr("selected", true); //设置Select的Text值为jQuery的项选中

jQuery添加/删除Select的Option项

 $("#select_id").append("<option value='Value'>Text</option>"); //为Select追加一个Option(下拉项) 
 $("#select_id").prepend("<option value='0'>请选择</option>"); //为Select插入一个Option(第一个位置) 
 $("#select_id option:last").remove(); //删除Select中索引值最大Option(最后一个) 
 $("#select_id option[index='0']").remove(); //删除Select中索引值为0的Option(第一个) 
 $("#select_id option[value='3']").remove(); //删除Select中Value='3'的Option 
 $("#select_id option[text='4']").remove(); //删除Select中Text='4'的Option 
 $("#SelectID").remove(); //删除所有项

Checkbox

 全选/取消 并获取/设置对象的属性值

$("input[name='item_list']").attr("checked"); //读取所有name为'item_list'对象的状态(是否选中) 
$("input[name='item_list']").attr("checked",true); //设置所有name为'item_list'对象的checked为true

获取上面实例中选中的checkbox的value值

<script type="text/javascript"> 
    var arrChk=$("input[name='item_list'][checked]"); 
    $(arrChk).each(function(){ 
      alert(this.value); 
    }); 
</script>

获取checkbox的value 

$("#checkboxID").val()或$("input[type='checkbox']").eq(n).attr("checked").value

yxkong

这个人很懒,什么都没留下

文章评论