【问题标题】:How to disable input text while the radio button is checked in JavaScript?在 JavaScript 中选中单选按钮时如何禁用输入文本?
【发布时间】:2017-02-04 13:13:08
【问题描述】:

这是在我的注册表单中,当我选中其中一个单选按钮时,应禁用带有占位符“指定”的输入类型文本。除非我选中单选按钮“其他”。这是为了指定注册人的位置。

<label for="position"><b>Position:</b></label><br/><br/>
<input type="radio" name="position" id="r1" value="Dean" />Dean
<input type="radio" name="position" id="r2" value="Instructor"/>Instructor
<input type="radio" name="position" id="r3" value="Student"/>Student<br/><br/>
<input type="radio" name="position" id ="r4" value="<?php echo $_POST['otherPosition']?>"/>
Others: 
<input type="text"  value = "<?php if($error==TRUE){echo $_POST['otherPosition'];}?>" id="otherPosition" placeholder="Specify" name="position"/>

这是我的 Javascript

<script>    
    if(document.getElementById('r1').checked||document.getElementById('r2').checked||document.getElementById('r3').checked){
        document.getElementById('otherPosition').readOnly = true;
    }else{
        document.getElementById('otherPosition').readOnly = false;
    }
</script>

【问题讨论】:

标签: javascript html css


【解决方案1】:

在 HTML 和 Javascript 中使用 onclick 函数

方法一:使用 PHP 值

HTML

<label for="position"><b>Position:</b></label><br/><br/>
<input type="radio" name="position" id="r1" value="Dean" onclick="Checkradiobutton()"/>Dean
<input type="radio" name="position" id="r2" value="Instructor" onclick="Checkradiobutton()"/>Instructor
<input type="radio" name="position" id="r3" value="Student" onclick="Checkradiobutton()"/>Student<br/><br/>
<input type="radio" name="position" id ="r4" value="<?php echo $_POST['otherPosition']?>" onclick="Checkradiobutton()"/>
Others: 
<input type="text"  value = "<?php if($error==TRUE){echo $_POST['otherPosition'];}?>" id="otherPosition" placeholder="Specify" name="position" />

脚本

 function Checkradiobutton()
 {

  if(document.getElementById('r1').checked || document.getElementById('r2').checked || document.getElementById('r3').checked)
 {

        document.getElementById('otherPosition').disabled=true; 
   }else{
                    document.getElementById('otherPosition').disabled = false;
                }
 }

以下片段示例

 function Checkradiobutton()
 {
  
  if(document.getElementById('r1').checked || document.getElementById('r2').checked || document.getElementById('r3').checked)
 {

        document.getElementById('otherPosition').disabled=true; 
   }else{
                    document.getElementById('otherPosition').disabled = false;
                }
 }
<label for="position"><b>Position:</b></label><br/><br/>
<input type="radio" name="position" id="r1" value="Dean" onclick="Checkradiobutton()"/>Dean
<input type="radio" name="position" id="r2" value="Instructor" onclick="Checkradiobutton()"/>Instructor
<input type="radio" name="position" id="r3" value="Student" onclick="Checkradiobutton()"/>Student<br/><br/>
<input type="radio" name="position" id ="r4" value="<?php echo $_POST['otherPosition']?>" onclick="Checkradiobutton()"/>
Others: 
<input type="text"  value = "<?php if($error==TRUE){echo $_POST['otherPosition'];}?>" id="otherPosition" placeholder="Specify" name="position" />

方法 2:直接 HTML 值

此示例删除您的 PHP 值并设置自定义值仅了解目的。

 function Checkradiobutton()
 {
  
  if(document.getElementById('r1').checked || document.getElementById('r2').checked || document.getElementById('r3').checked)
 {

        document.getElementById('otherPosition').disabled=true; 
   }else{
                    document.getElementById('otherPosition').disabled = false;
                }
 }
<label for="position"><b>Position:</b></label><br/><br/>
<input type="radio" name="position" id="r1" value="Dean" onclick="Checkradiobutton()"/>Dean
<input type="radio" name="position" id="r2" value="Instructor" onclick="Checkradiobutton()"/>Instructor
<input type="radio" name="position" id="r3" value="Student" onclick="Checkradiobutton()"/>Student<br/><br/>
<input type="radio" name="position" id ="r4" value="Other" onclick="Checkradiobutton()"/>
Others: 
<input type="text"  value = "OtherPoistion" id="otherPosition" placeholder="Specify" name="position" />

【讨论】:

    猜你喜欢
    • 2020-05-12
    • 2019-07-24
    • 2021-05-14
    • 2012-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多