根据无线电输入条件禁用和启用按钮提交

Disabling and enabling button submit based on radio input conditions

本文关键字:启用 按钮 提交 无线电 输入 条件      更新时间:2023-09-26

如果用户没有选择单选按钮,我想禁用输入字段的可点击性。这是简单的HTML表单:

<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>

现在,我做了这个js,看看是否选择了其中一个输入,那么disabled="disabled"部分应该受到尊重,但现在在这个JavaScript代码中就是这种情况。

if(document.getElementById('checkMe').checked) {
   document.getElementById('choose').disabled=false;
}  

这是在线演示。 http://jsfiddle.net/2HC6s/

试试这个 | 演示

<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>

把它放在一张桌子里,然后在她身上做:

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>