jquery 遍历radio,checkbox,select等并设置值

1. radio 操作

   

<input type="radio" name="radiogroup1_${pu[0]}" value="1"/>集成厂商
<input type="radio" name="radiogroup1_${pu[0]}" value="2"/>网建部普工
<input type="radio" name="radiogroup1_${pu[0]}" value="3"/>网建项目经理
<input type="radio" name="radiogroup1_${pu[0]}" value="4"/>运维部普工
<input type="radio" name="radiogroup1_${pu[0]}" value="5"/>运维项目经理
  a. 实现传递value值,选中对应的radio选项。

    

//id为${pu[0]} 值,value 为 radio 的value值
function editUserRole(id,value){

	$("input:[name=radiogroup1_"+id+"]").each(function(){
			if($(this).val()==value){
				$(this).attr('checked','true');
			}
	});
}
b. 获取radio的value值

  

$('input:radio:checked').val();

$("input[type='radio']:checked").val();

$("input[name='rd']:checked").val();


c.设置第一个Radio为选中值:

    $('input:radio:first').attr('checked', 'checked');

或者

$('input:radio:first').attr('checked', 'true');

注:attr("checked",'checked')= attr("checked", 'true')= attr("checked", true)


3.设置最后一个Radio为选中值:

$('input:radio:last').attr('checked', 'checked');

或者

$('input:radio:last').attr('checked', 'true');

4.根据索引值设置任意一个radio为选中值:

$('input:radio').eq(索引值).attr('checked', 'true');索引值=0,1,2....

或者

$('input:radio').slice(1,2).attr('checked', 'true');

5.根据Value值设置Radio为选中值

$("input:radio[value=http://www.2cto.com/kf/201110/'rd2']").attr('checked','true');

或者

$("input[value=http://www.2cto.com/kf/201110/'rd2']").attr('checked','true');

6.删除Value值为rd2的Radio

$("input:radio[value=http://www.2cto.com/kf/201110/'rd2']").remove();

7.删除第几个Radio

$("input:radio").eq(索引值).remove();索引值=0,1,2....

如删除第3个Radio:$("input:radio").eq(2).remove();

8.遍历Radio

$('input:radio').each(function(){

     //写入代码

});

遍历指定radio 组的值

$("input:[name=radiogroup1_"+id+"]").each(function(){
if($(this).val()==value){
$(this).attr('checked','true');
}
});


2. select 选中值操作

1.   获取选中项:

获取选中项的Value值:

      $('select#sel option:selected').val();

     或者

       $('select#sel').find('option:selected').val();

获取选中项的Text值:

      $('select#seloption:selected').text();

     或者

       $('select#sel').find('option:selected').text();

2.   获取当前选中项的索引值:

$('select#sel').get(0).selectedIndex;

3.   获取当前option的最大索引值:

$('select#sel option:last').attr("index")

4.   获取DropdownList的长度:

$('select#sel')[0].options.length;

或者

$('select#sel').get(0).options.length;

5.  设置第一个option为选中值:

$('select#sel option:first').attr('selected','true')

或者

 $('select#sel')[0].selectedIndex = 0


jQuery获取Select选择的TextValue:

语法解释:
1. $("#select_id").change(function(){//code...});   //Select添加事件,当选择其中一项时触发
2. var checkText=$("#select_id").find("option:selected").text();  //获取Select选择的Text

或者 $("#select1  option:selected").text();

3. var checkValue=$("#select_id").val();  //获取Select选择的Value
4. var checkIndex=$("#select_id ").get(0).selectedIndex;  //获取Select选择的索引值
5. var maxIndex=$("#select_id option:last").attr("index");  //获取Select最大的索引值 

    
jQuery设置Select选择的TextValue:
语法解释:
1. $("#select_id ").get(0).selectedIndex=1;  //设置Select索引值为1的项选中
2. $("#select_id ").val(4);   //设置SelectValue值为4的项选中
3. $("#select_id option[text='jQuery']").attr("selected", true);   //设置SelectText值为jQuery的项选中

jQuery添加/删除SelectOption项:

 点击一次,Select将追加一个Option
 点击将在Select第一个位置插入一个Option
 点击将删除Select中索引值最大Option(最后一个)
1. $("#select_id").append("<option value='Value'>Text</option>");  //Select追加一个Option(下拉项)
2. $("#select_id").prepend("<option value='0'>请选择</option>");  //Select插入一个Option(第一个位置)
3. $("#select_id option:last").remove();  //删除Select中索引值最大Option(最后一个)
4. $("#select_id option[index='0']").remove();  //删除Select中索引值为0Option(第一个)
5. $("#select_id option[value='3']").remove();  //删除SelectValue='3'Option
5. $("#select_id option[text='4']").remove();  //删除SelectText='4'Option

3,复选框:
$("input[@type=checkbox][@checked]").val(); //得到复选框的选中的第一项的值
$("input[@type=checkbox][@checked]").each(function(){ //由于复选框一般选中的是多个,所以可以循环输出
alert($(this).val());
});

$("#chk1").attr("checked",'');//不打勾
$("#chk2").attr("checked",true);//打勾
if($("#chk1").attr('checked')==undefined){} //判断是否已经打勾

 jQuery(function () {

  jQuery("#selectAllcbx").click(function(){
  if(jQuery("#selectAllcbx").attr("checked")==true){
        jQuery("[name='cbx']").attr("checked",'true');//全选
        jQuery("#delSelectBtn").css("display","");
        } else {
        jQuery("[name='cbx']").removeAttr("checked");//取消全选
        jQuery("#delSelectBtn").css("display","none");
        }
  });
 

});


<input type="checkbox" id="selectAllcbx" name="selectAllcbx" value="selectAllcbx"> 

<input type="checkbox" id="cbx" name="cbx" value="1">

<input type="checkbox" id="cbx" name="cbx" value="2">

<input type="checkbox" id="cbx" name="cbx" value="3">



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值