admin管理员组文章数量:1291583
I would like to disable an input field from being click-able if user hasn't selected a radio button. Here is the simple HTML form:
<form method="POST">
<input type='radio' name='a' value='a' id='checkMe' /> a
<input type='radio' name='a' value='b' id='checkMe' /> b
<input type='radio' name='a' value='c' id='checkMe' /> c
<input type='submit' value='choose' id='choose' disabled="disabled"/>
</form>
Now, I made this js, to see if one of the inputs is selected, then the disabled="disabled"
part should be revered, but that is now the case in this JavaScript code
if(document.getElementById('checkMe').checked) {
document.getElementById('choose').disabled=false;
}
Here is the online demo. /
I would like to disable an input field from being click-able if user hasn't selected a radio button. Here is the simple HTML form:
<form method="POST">
<input type='radio' name='a' value='a' id='checkMe' /> a
<input type='radio' name='a' value='b' id='checkMe' /> b
<input type='radio' name='a' value='c' id='checkMe' /> c
<input type='submit' value='choose' id='choose' disabled="disabled"/>
</form>
Now, I made this js, to see if one of the inputs is selected, then the disabled="disabled"
part should be revered, but that is now the case in this JavaScript code
if(document.getElementById('checkMe').checked) {
document.getElementById('choose').disabled=false;
}
Here is the online demo. http://jsfiddle/2HC6s/
Share Improve this question asked Sep 18, 2013 at 13:08 samayosamayo 16.5k13 gold badges93 silver badges113 bronze badges 1- 1 you cannot have the same ID for several elements – Elen Commented Sep 18, 2013 at 13:09
4 Answers
Reset to default 3Try this | demo
<form method="POST" id="question">
<input type='radio' name='a' value='a' id='checkMe' onclick="check()"/> a
<input type='radio' name='a' value='b' id='checkMe1' onclick="check()" /> b
<input type='radio' name='a' value='c' id='checkMe2' onclick="check()" /> c
</br>
function check()
{
var ele = document.getElementsByName('a');
var flag=0;
for(var i=0;i<ele.length;i++)
{
if(ele[i].checked)
flag=1;
}
if(flag==1)
document.getElementById('choose').disabled=false;
}
<!DOCTYPE html>
<html>
<head>
<title>Stack</title>
<script>
function set_btn_status()
{
var radios = document.getElementsByName("a");
for (var i = 0; i < radios.length; i++) {
if (radios[i].checked) {
var checked_value = radios[i].value;
if(checked_value == 'a') {
document.getElementById('choose').disabled = false;
} else {
document.getElementById('choose').disabled = true;
}
break;
}
}
}
</script>
</head>
<body>
<form method="POST">
<input type='radio' name='a' value='a' id='checkMe' onclick="set_btn_status()"/> a
<input type='radio' name='a' value='b' id='checkMe1' onclick="set_btn_status()"/> b
<input type='radio' name='a' value='c' id='checkMe2' onclick="set_btn_status()"/> c
<input type='submit' value='choose' id='choose' disabled="disabled"/>
</form>
</body>
</html>
Put it inside a table and then do on her:
var tabPom = document.getElementById("pomTableId");
$(tabPom ).prop('disabled', true/false);
<form method="POST" id="question">
<input type='radio' name='a' value='a' id='checkMe' /> a
<input type='radio' name='a' value='b' id='checkMe' /> b
<input type='radio' name='a' value='c' id='checkMe' /> c
</br>
<input type='submit' value='choose' id='choose' disabled="disabled"/>
</form>
<script>
document.getElementById('checkMe').onclick = function() {
document.getElementById('choose').disabled=false;
}
</script>
本文标签: javascriptDisabling and enabling button submit based on radio input conditionsStack Overflow
版权声明:本文标题:javascript - Disabling and enabling button submit based on radio input conditions - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741537471a2384112.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论