我有多个动态添加的单选按钮。我想将所选单选按钮的值属性更改为 "yes"
,并在选择其他单选按钮后将单选按钮值更改为 "no"
。
<input type="radio" name="tab-active" value="no" />
我可以使用以下 jQuery 获取更改为 "yes"
的值:
$(document).on("click","input[name=tab-active]:radio",function(){
if($("input:radio[name=tab-active]").is(":checked")) {
$(this).val('yes');
}
});
如何在选择其他单选按钮后将所选单选按钮的值属性更改为 "no"
?
最佳答案
使用 :not()选择器。而且,如您所见,您还可以使用 :checked 将值设置为 yes。
$(document).on("click","input[name=active]:radio",function(){
$("input:radio[name=active]:checked").val('yes');
$("input:radio[name=active]:not(:checked)").val('no');
});
关于javascript - 使用 jQuery 更改组中单选按钮的值属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20772816/