javascript 使用 jQuery 更改组中单选按钮的值属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20772816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing value attribute of radio buttons in a group using jQuery
提问by Addicted
I have multiple dynamically added radio buttons. I want to change the value attribute of selected radio button to "yes"
and change the radio button value to "no"
once some other radio button is selected.
我有多个动态添加的单选按钮。我想将选定单选按钮的值属性更改为,"yes"
并在"no"
选择其他单选按钮后将单选按钮值更改为。
<input type="radio" name="tab-active" value="no" />
I am able to get the value to change to "yes"
by using the following jQuery:
我可以"yes"
使用以下 jQuery获取要更改的值:
$(document).on("click","input[name=tab-active]:radio",function(){
if($("input:radio[name=tab-active]").is(":checked")) {
$(this).val('yes');
}
});
How can I change the value attribute of the selected radio button to "no"
once some other radio button is selected?
选择"no"
其他单选按钮后,如何将所选单选按钮的值属性更改为?
回答by Markus Kottl?nder
Use the :not()selector. And, as you see, you also can use :checked to set the value to yes.
使用: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');
});
回答by Mahesh Sapkal
回答by Rajesh Paul
$("input[name='tab-active']").change(function()
{
$("input[name='tab-active']").val('no');
$(this).val('yes');
}
)
回答by Bhavesh Parekh
you can make html tags like
你可以像这样制作html标签
<input type="radio" id="1" name="tab-active" /><span id="lbl1">no</span>
<input type="radio" id="2" name="tab-active" /><span id="lbl2">no</span>
<input type="radio" id="3" name="tab-active" /><span id="lbl3">no</span>
And the js code like
和 js 代码一样
$(document).on("click","input[type:radio]",function(){
if($("input:radio[name=tab-active]").is(":checked")) {
$("#lbl" + $(this).attr('id')).html("yes");
}
});
You can check it in this link http://jsfiddle.net/nkvak/
您可以在此链接http://jsfiddle.net/nkvak/ 中查看
Hope it will help you
希望它会帮助你