2014-02-13 82 views
1

我有2個單選按鈕,即是和否。當我選擇「是」時,文本框將被啓用。當我點擊沒有文字框被禁用。如何在點擊是時啓用文本框。這是代碼。請告訴我如何使用JavaScript啓用和禁用它。僅當單擊單選按鈕1時啓用文本框

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

回答

0

首先使文本框禁用。

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

單擊單選按鈕時,檢查並啓用它。

if(document.getElementById('XIyes').checked) { 
    document.getElementById("XIyop").disabled = false; 
    }else if(document.getElementById('XIno').checked) { 
     document.getElementById("XIyop").disabled = true; 
    } 
2

我想,你應該使用一些這方面的一般處理程序:http://jsfiddle.net/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(); 
}); 
0
$(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> 
0
if(document.getElementById('XIyes').attr('checked')) { 
    document.getElementById("XIyop").disabled = 'true'; 
} 

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

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

請參閱工作演示here。謝謝,我希望這會幫助你。

相關問題