1

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.net/2HC6s/

4

4 回答 4

3

试试这个| 演示

<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;
}
于 2013-09-18T13:15:42.400 回答
2
<!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>
于 2013-09-18T13:12:01.143 回答
1

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

var tabPom = document.getElementById("pomTableId");
$(tabPom ).prop('disabled', true/false);
于 2013-11-04T13:02:05.787 回答
0
<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>
于 2018-05-05T12:29:58.247 回答