【问题标题】:HTML radio input with (required) text option for "other"带有“其他”的(必需)文本选项的 HTML 单选输入
【发布时间】:2016-01-30 16:55:27
【问题描述】:

我有一组带有“其他”选项的无线电输入,该选项有一个文本字段。我的问题是如何在选择其他选项时进行文本字段。

到目前为止我想出的代码::

<form method="POST" action="testPage.php">
  <input type="radio" name="option" value="1">Option 1<br>
  <input type="radio" name="option" value="2">Option 2<br>
  <input type="radio" name="option" value="3">Option 3<br>
  <input type="radio" name="option" value="">Other 
  <input type="text" name="othertext" onchange="changeradioother()">
  <input type="submit" value="Submit">
</form>

<script>

  function changeradioother(){
  var other= document.getElementById("other");
  other.value=document.getElementById("inputother").value;
  }

</script>

【问题讨论】:

    标签: javascript html forms radio-button


    【解决方案1】:

    你可以做这样的事情。 遍历所有单选按钮以检查哪个被点击。

    顺便说一句,您没有在单选按钮Other 上添加id

    var radio = document.querySelectorAll("input[name='option']");
    
    for(var i = 0;i< radio.length;i++){
      radio[i].onclick = function(){
        if(this.id == "other"){
          document.getElementById('otherText').setAttribute("required", true);
        }else{
          document.getElementById('otherText').removeAttribute("required");
        }
      }
    }
    <form method="POST" action="testPage.php">
      <input type="radio" name="option" value="1">Option 1<br>
      <input type="radio" name="option" value="2">Option 2<br>
      <input type="radio" name="option" value="3">Option 3<br>
      <input type="radio" name="option" id="other">Other 
      <input type="text" name="othertext" id="otherText">
      <input type="submit" value="Submit">
    </form>

    【讨论】:

      【解决方案2】:

      这里有很多不同的方法 为您的单选框提供 id,这是其他的,并为文本框提供 id。

       function changeradioother(){
       
        if(document.getElementById("Other").checked ) {
           if(document.getElementById("inputtext").value == ''){
               alert('input required');
           }
        }
           return false
        }
      <form method="POST" action="testPage.php" onsubmit="return changeradioother()">
        <input type="radio" name="option" value="1">Option 1<br>
        <input type="radio" name="option" value="2">Option 2<br>
        <input type="radio" name="option" value="3">Option 3<br>
        <input type="radio" name="option" value="" id="Other">Other 
        <input type="text" name="othertext" id="inputtext" >
        <input type="submit" value="Submit">
      </form>

      【讨论】:

      • 你认为这是javascript吗?它是jquery
      • 不,这是javascript函数中的jquery代码
      • 查看 Op 标签。我的意思是人们可以对您的答案投反对票,因此将其更改为 javascript。或建议您的答案是jquery
      • 抱歉,我会改正的,谢谢提问
      【解决方案3】:

      尝试调用函数when other radio button is clicked 来设置input field to required

      <script>
      
      function changeradioother(){
      	  var other= document.getElementById("other");
      	  other.value=document.getElementById("inputother").value;
      	  }
      function setRequired(){
      
      	document.getElementById("inputother").required=true;
      }
      
      function removeRequired(){
      if(document.getElementById("inputother").required == true){
      	document.getElementById("inputother").required=false;
      }
      }
      </script>
      
      <form method="POST" action="testPage.php">
        <input type="radio" name="option" value="1" onclick="removeRequired();">Option 1<br>
        <input type="radio" name="option" value="2" onclick="removeRequired();">Option 2<br>
        <input type="radio" name="option" value="3" onclick="removeRequired();">Option 3<br>
        <input type="radio" name="option" value="" onclick="setRequired();" id="other">Other 
        <input id="inputother" type="text" name="othertext" onchange="changeradioother()">
        <input type="submit" value="Submit">
      </form>

      【讨论】:

      • 当您第一次单击其他,然后返回任何其他选项时,它仍然是必填字段。
      • @Ivar 好点,即使 Op 从未要求将其更改为正常,但我会更改它。谢谢。
      • 成功了!我喜欢这个,因为它没有打开警报框。谢谢大家的回答
      猜你喜欢
      • 1970-01-01
      • 2018-08-17
      • 1970-01-01
      • 2021-12-29
      • 1970-01-01
      • 2011-08-04
      • 2020-12-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多