【问题标题】:Radio button text area单选按钮文本区域
【发布时间】:2016-10-30 13:45:47
【问题描述】:

我有这份调查表,效果很好。我只需要改变这一个问题。基本上有 2 个单选按钮用于回答“是”和“否”以及它们下方的文本区域。现在我希望文本区域被锁定,除非用户选择“是”单选按钮,然后他们可以在文本区域中输入“是”的原因。

我环顾四周并尝试了这个功能,但它似乎不起作用。

<script>
  function validate() {
    var radioYes = document.getElementById('radioYes');
    var textarea = document.getElementById('question5comment');

    if (radioYes.checked && question5comment.value.length < 1) {
      alert('Please enter your reason for Question 5.');
      question5comment.focus();
      return false;
    }
  }

  function toggle(value) {
    document.getElementById('question5comment').disabled = value;
  }
</script>

5. Are you using other non-franchise service centres? 
<br>
*if yes is there any other reason you would do so other than price
<br>
<input type="radio" name="question5"
       value="Yes" required>Yes
<br>
<input type="radio" name="question5"
       <value="No" required>No 
<br>
<textarea name="question5comment"  rows="5" cols="40" required></textarea>

【问题讨论】:

    标签: php html


    【解决方案1】:

    你有很多缺失的代码!像 id not exist 一样,创建了函数但没有调用它的初始化。

    <script>
    function validate(t) {
    var question5comment = document.getElementById('question5comment');    
    if (t.checked && question5comment.value.length < 1) {
        alert('Please enter your reason for Question 5.');
        question5comment.focus();
        return false;
    }
    toggle(t.value);
    }
    
    function toggle(value) {
        document.getElementById('question5comment').disabled = (value == "Yes") ? false : true;
    }
    </script>
    
    
    5. Are you using other non-franchise service centres? 
    <br>
    *if yes is there any other reason you would do so other than price
    <br>
    <input type="radio" name="question5"
    value="Yes" onchange="validate(this)" required>Yes
    <br>
    <input type="radio" name="question5" onchange="validate(this)" value="No" required>No 
    <br>
    <textarea name="question5comment" id="question5comment" rows="5" cols="40" required></textarea>
    

    【讨论】:

      【解决方案2】:

      你的语法错误:

      1. value 之前不需要&lt;
      2. 没有在您的代码中引用的ids。
      3. 语法错误。

      使用以下内容:

      function validate() {
        var radioYes = document.getElementById('radioYes');
        var textarea = document.getElementById('question5comment');
      
        if (radioYes.checked && question5comment.value.length < 1) {
          alert('Please enter your reason for Question 5.');
          question5comment.focus();
          document.getElementById('question5comment').disabled = true;
          return false;
        } else {
          document.getElementById('question5comment').disabled = false;
        }
      }
      5. Are you using other non-franchise service centres?
      <br>*if yes is there any other reason you would do so other than price
      <br>
      <input type="radio" name="question5" value="Yes" required onclick="validate();" id="radioYes" />Yes
      <br>
      <input type="radio" name="question5" value="No" required onclick="validate();" id="radioNo" />No
      <br>
      <textarea name="question5comment" id="question5comment" rows="5" cols="40" required></textarea>

      【讨论】:

      • 那行得通,只需要改变真假。但我希望只有在他们尝试提交而不在文本区域中输入任何内容的情况下才会弹出警报。先生,这可能吗?
      • 是的,在您的提交处理程序中,检查值的长度,如果是的话。
      • 完美的先生,我准备好了。最后一件事是我可以默认锁定textarea吗?所以当表单打开时文本区域被锁定
      • @EternalJ 只需在开头添加textarea.disabled = true;:)
      猜你喜欢
      • 2014-11-29
      • 1970-01-01
      • 1970-01-01
      • 2012-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多