jquery的checkbox,radio,select常用用法

1、checkbox日常jquery操作。


以下面的html为例进行checkbox的操作

<input id="checkAll" type="checkbox" />全选
<input name="subBox" type="checkbox" />项1
<input name="subBox" type="checkbox" />项2
<input name="subBox" type="checkbox" />项3
<input name="subBox" type="checkbox" />项4

全选和全不选:

<script type="text/javascript">
   //要先引入jQuery文件
   $(function() {
      $("#checkAll").click(function() {
           $('input[name="subBox"]').attr("checked",this.checked); 
       });
       var $subBox = $("input[name='subBox']");
       $subBox.click(function(){
           $("#checkAll").attr("checked",$subBox.length == $("input[name='subBox']:checked").length ? true : false);
       });
   });
</script>

checkbox属性:

var val = $("#checkAll").val();// 获取指定id的复选框的值
var isSelected = $("#checkAll").attr("checked");//判断id=checkAll的复选框是否处于选中,true:是,false:否
$("#checkAll").attr("checked", true);//全选按钮置为选中
$("#checkAll").attr("checked", 'checked');//同上 
$("#checkAll").attr("checked", false);//id='checkAll' 取消选中 
$("#checkAll").attr("checked", '');//同上 
$("input[name=subBox][value=3]").attr("checked", 'checked');// 将name=subBox, value=3 置为选中
$("input[name=subBox][value=3]").attr("checked", '');//将name=subBox, value=3取消选中
$("input[type=checkbox][name=subBox]").get(2).checked = true;// 设置index = 2,即第三项为选中状态  
$("input[type=checkbox]:checked").each(function(){ //由于复选框一般选中的是多个,所以可以循环输出选中的值  
    alert($(this).val());  
});

2、radio的jquery日常操作及属性


仍然以下面的html为例:

<input type="radio" name="radio" id="radio1" value="1" />1  
<input type="radio" name="radio" id="radio2" value="2" />2  
<input type="radio" name="radio" id="radio3" value="3" />3  
<input type="radio" name="radio" id="radio4" value="4" />4 

radio操作如下:

$("input[name=radio]:eq(0)").attr("checked",'checked'); //第一个radio选中
$("#radio1").attr("checked","checked");//id="radio" 置为选中
$("#radio1").removeAttr("checked");//取消选中
$("input[type='radio'][name='radio']:checked").length == 0 ? "未选中" : "有选中";//类似于java的三元运算
$('input[type="radio"][name="radio"]:checked').val(); // 获取一组radio被选中项的值  
$("input[type='radio'][name='radio'][value='2']").attr("checked", "checked");//设置value=2的为选中
$("#radio2").attr("checked", "checked"); // 设置id=radio2的一项为选中  
$("input[type='radio'][name='radio']").get(1).checked = true; // 设置index = 1,即第二项为当前选中  
var isChecked = $("#radio2").attr("checked");// 判断id=radio2是否处于选中,true:是 false:否 
var isChecked = $("input[type='radio'][name='radio'][value='2']").attr("checked");//同上

3、select下拉框的日常jquery操作


select操作相比checkbox和radio要相对麻烦一些,仍然以下面的html为例来说明

<select name="select" id="select_id" style="width: 100px;">  
    <option value="1">11</option>  
    <option value="2">22</option>  
    <option value="3">33</option>  
    <option value="4">44</option>  
    <option value="5">55</option>  
    <option value="6">66</option>  
</select>

看select的如下属性:

$("#select_id").change(function(){  // 1.为Select添加事件,当选择其中一项时触发   
     //code...  
 });  
 var checkValue = $("#select_id").val();                    // 2.获取Select选中项的Value  
 var checkText = $("#select_id :selected").text();          // 3.获取Select选中项的Text   
 var checkIndex = $("#select_id").attr("selectedIndex");    // 4.获取Select选中项的索引值,
 var checkIndex = $("#select_id").get(0).selectedIndex;        //同上
 var maxIndex =$("#select_id :last").get(0).index;        // 5.获取Select最大的索引值
 /** 
  * jQuery设置Select的选中项 
  */  
 $("#select_id").get(0).selectedIndex = 1;       // 1.设置Select索引值为1的项选中  
 $("#select_id").val(4);                         // 2.设置Select的Value值为4的项选中  
 /** 
 * jQuery添加/删除Select的Option项 
 */  
$("#select_id").append("<option value='新增'>新增option</option>");// 1.为Select追加一个Option(下拉项)   
$("#select_id").prepend("<option value='0'>00</option>");// 2.为Select插入一个Option(第一个位置)  
$("#select_id").get(0).remove(1);      // 3.删除Select中索引值为1的Option(第二个)  
$("#select_id :last").remove();        // 4.删除Select中索引值最大Option(最后一个)   
$("#select_id [value='3']").remove();  // 5.删除Select中Value='3'的Option   
$("#select_id").empty();            //清空select 

$("#select_id").find("option:selected").text(); // 获取select 选中的 text :
$("#select_id").val(); // 获取select选中的 value:
$("#select_id").get(0).selectedIndex; // 获取select选中的索引:

//设置select 选中的value:
$("#select_id").attr("value","Normal");
$("#select_id").val("Normal");
$("#select_id").get(0).value = value;

//设置select 选中的text,通常可以在select回填中使用
var numId=33 //设置text==33的选中!
var count=$("#select_id  option").length;
for(var i=0;i<count;i++) {
    if($("#select_id").get(0).options[i].text == numId){  
         $("#select_id").get(0).options[i].selected = true;  
         break;  
     }  
 }
//当然还有更简单的
$("#select_id").val("5");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值