javascript 单击后禁用单选按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5892394/
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
Disabling radio button after click
提问by user735205
I need some help please This is the html
我需要一些帮助 这是 html
<div>
<p>match1</p>
teamA <input type="radio" name="match1" onclick="update('ab');" />
teamB <input type="radio" name="match1" onclick="update('ba');" />
<p>match2</p>
teamC <input type="radio" name="match1" onclick="update('ad');" />
teamD <input type="radio" name="match1" onclick="update('dc');" />
</div>
<script>
update(results){.................}
</script>
I have this html so what I want I to know is
How can I disable the radio button once the user clicks on it
because the update() functionchanges the values when user clicks on radio button
if he keeps clicking like that then values change each time he clicks
or if he clicks on sibling radio button
so please can anyone tell me how to disable radio button
once user clicks on it
like for instance in match1
if user selects teamA radio button then i want to disable both teamA and teamB radio buttons
same for match2 if he clicks then disable the clciked radio and sibling radio aswell
我有这个 html,所以我想知道的是
如何在用户单击后禁用单选按钮,
因为update() 函数会在用户单击单选按钮时更改值,
如果他一直这样单击,则每个值都会更改一次他点击
,或者如果他点击同级单选按钮,
所以请谁能告诉我如何禁用单选按钮
就可以了,一旦用户点击
例如像在MATCH1
如果用户选择teamA单选按钮,然后我要禁用两台teamA和teamB单选按钮
相同对于 match2,如果他点击然后禁用 clciked 收音机和兄弟收音机
Thanks for reading this can anyone help me please
I can use plain js, jquery or libraries
感谢阅读本文,任何人都可以帮助我,
我可以使用普通的 js、jquery 或库
采纳答案by Edgar Villegas Alvarado
Use this:
用这个:
$(":radio").click(function(){
var radioName = $(this).attr("name"); //Get radio name
$(":radio[name='"+radioName+"']").attr("disabled", true); //Disable all with the same name
});
What we're doing, is, when a user clicks a radio button, disable all radios with the same name.
我们正在做的是,当用户单击一个单选按钮时,禁用所有同名的单选按钮。
Hope this helps. Cheers.
希望这可以帮助。干杯。
回答by kobe
jsfiddle example
jsfiddle 示例
code
代码
$(":radio").click(function(){
$(this).attr('disabled','disabled');
});
this is the general way of doing it.
这是这样做的一般方法。
jQuery("input:radio").attr('disabled',true);
or
或者
jQuery("input:radio").attr('disabled','disabled');
回答by Jesufer Vn
Using Javascript, you can use a solution like this:
使用 Javascript,您可以使用如下解决方案:
document.getElementById("IDOfButtonElement").onclick = function(){
document.getElementById("IDOfButtonElement").disabled=true;
}
回答by Atticus
onclick="update('ad', this);"
function onclick(word, element){
$(element).attr('disabled', 'disabled');
}
回答by Mohsen
Try this:
试试这个:
this.setAttribute('disabled','disabled');
I suggest you to add label
tag around your inputs. It cause when user clicks on the text radio button changes.
我建议您label
在输入周围添加标签。它会导致用户单击文本单选按钮时发生变化。
<label>teamA <input type="radio" name="match1" onclick="update('ab');" /></label>