admin管理员组

文章数量:1327843

I have 2 radio button ie yes and no. When I select yes the text box as to get enabled. When i click no the text box as to be disabled. How to enable the text box when I click on Yes. Here is the code. Please tel me how to enable and disable it using javascript.

<script type="text/javascript">
$(function() {

    $("#XISubmit").click(function(){

        var XIyop= document.forms["XIForm"]["XIyop"].value;
        var XIForm = $('form[name=XIForm]');
        var XIAlmnus = XIForm.find('input[name=XIAlmnus]:checked').val();

        if (XIAlmnus == null || XIAlmnus == "") 
        {
            alert("Please select  Parent is an Alumnus (old Boy) of this school");
            return false;
        }
        document.getElementById("XIForm").submit();
    });
</script>        

<!-- html code-->
<html>
...
<label>Parent is an Alumnus (old Boy) of this school </label> &nbsp&nbsp
<input type='radio' name='XIAlmnus' value='Yes' id="XIyes"/>Yes
<input type='radio' name='XIAlmnus' value='No' id="XIno"/>No</td>

<label>If Yes, Year of passing </label> &nbsp&nbsp
<input type="textbox" name="XIyop" id="XIyop" >
...
</html>

I have 2 radio button ie yes and no. When I select yes the text box as to get enabled. When i click no the text box as to be disabled. How to enable the text box when I click on Yes. Here is the code. Please tel me how to enable and disable it using javascript.

<script type="text/javascript">
$(function() {

    $("#XISubmit").click(function(){

        var XIyop= document.forms["XIForm"]["XIyop"].value;
        var XIForm = $('form[name=XIForm]');
        var XIAlmnus = XIForm.find('input[name=XIAlmnus]:checked').val();

        if (XIAlmnus == null || XIAlmnus == "") 
        {
            alert("Please select  Parent is an Alumnus (old Boy) of this school");
            return false;
        }
        document.getElementById("XIForm").submit();
    });
</script>        

<!-- html code-->
<html>
...
<label>Parent is an Alumnus (old Boy) of this school </label> &nbsp&nbsp
<input type='radio' name='XIAlmnus' value='Yes' id="XIyes"/>Yes
<input type='radio' name='XIAlmnus' value='No' id="XIno"/>No</td>

<label>If Yes, Year of passing </label> &nbsp&nbsp
<input type="textbox" name="XIyop" id="XIyop" >
...
</html>
Share edited Feb 13, 2014 at 11:14 cavalsilva 19413 bronze badges asked Feb 13, 2014 at 11:06 user3286350user3286350 892 gold badges3 silver badges12 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 2

I think, you should use some general handler for this: http://jsfiddle/maximgladkov/MvLXL/

$(function() {
    window.invalidate_input = function() {
        if ($('input[name=XIAlmnus]:checked').val() == "Yes")
            $('#XIyop').removeAttr('disabled');
        else
            $('#XIyop').attr('disabled', 'disabled');
    };

    $("input[name=XIAlmnus]").change(invalidate_input);

    invalidate_input();
});

first make the text box disabled.

<input type="textbox" name="XIyop" id="XIyop" disabled>    

When radio button clicked, check and enable it.

if(document.getElementById('XIyes').checked) {
     document.getElementById("XIyop").disabled = false;
    }else if(document.getElementById('XIno').checked) {
      document.getElementById("XIyop").disabled = true;
    }
$(function() {
                $('input[name="XIAlmnus"]').on('change', function() {
                    if ($(this).val() == 'Yes') {
                        $("#XIyop").prop('disabled', false);
                    } else {
                        $("#XIyop").prop('disabled', true);
                    }
                });
            });

<input type="textbox" name="XIyop" id="XIyop" disabled>
if(document.getElementById('XIyes').attr('checked')) {
    document.getElementById("XIyop").disabled = 'true';
} 

if(document.getElementById('XIno').attr('checked')) {
    document.getElementById("XIyop").disabled = 'false';
} 

HTML:

<label>Parent is an Alumnus (old Boy) of this school </label> &nbsp&nbsp
<input type='radio' name='XIAlmnus' value='Yes' id="XIyes"/>Yes
<input type='radio' name='XIAlmnus' value='No' id="XIno"/>No
<br/>
<label>If Yes, Year of passing </label> &nbsp&nbsp
<input type="textbox" name="XIyop" id="XIyop" disabled>

JS:

document.getElementById('XIyes').onchange = displayTextBox;
document.getElementById('XIno').onchange = displayTextBox;

var textBox = document.getElementById('XIyop');

function displayTextBox(evt){
    if(evt.target.value=="Yes"){
        textBox.disabled = false;
    }else{
        textBox.disabled = true;
    }
}

Please see working demo here. Thank you I hope this will help you.

本文标签: javascriptEnable a text box only when 1 of the radio button is clickedStack Overflow