1.获取选中值,三种方法都可以:
$('input:radio:checked').val();
$("input[type='radio']:checked").val();
$("input[name='rd']:checked").val();
2.设置第一个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....
4.value = 该值为radio的动态需要设置选中的值//
$("input[type=radio][name=radioType][value='"+value+"']").attr("checked",true);
第一次确实是可以选中,但是当两个选中相互切换的时候就会出现即使你设置了attr("checked",false)会失效的情况
正确方式:
if(value == '1'){
$('.testSelectOne').prop("checked", "checked");
$('.testSelectTwo').prop("checked", "");
}else if(value == '0'){
$('.testSelectOne').prop("checked", "");
$('.testSelectTwo').prop("checked", "checked");
}
必须使用prop,不能使用attr属性否则不能成功。对于这种像checkbox,radio和select这样的元素,选中属性对应“checked”和“selected”,这些也属于固有属性,因此需要使用prop方法去操作才能获得正确的结果。
注意:
在编辑按钮radio时,要注意name属性的值一定要相同;
初始化让单选框选中有4种方法可以实现:
1、checked
2、checked='checked'(建议用此方法,此方法对浏览器可以有很好的兼容性)
3、checked='true'
4、checked=true
option的值
<select id="testSelect " name="" class="valid">
<option value="1">text1</option>
<option value="2">text2</option>
</select>
1.给特定value值得option设定选中
var type=1;
$(".valid option[value='"+type+"']").attr("selected",true);
2.获取选中的值
javascript原生的方法
1:拿到select对象: var myselect=document.getElementById("testSelect ");
2:拿到选中项的索引:var index=myselect.selectedIndex ; //
selectedIndex代表的是你所选中项的index
3:拿到选中项options的value: myselect.options[index].value;
4:拿到选中项options的text: myselect.options[index].text;
jq方法
$('#testSelect option:selected').text();//选中的文本
$('#testSelect option:selected') .val();//选中的值
$("#testSelect ").get(0).selectedIndex;//索引
$("#tesetSelect").find("option:selected").text();//选中的文本
…….val();
…….get(0).selectedIndex;
祝工作顺利,身体健康