单选按钮文本区域

时间:2016-06-28 09:46:08

标签: php html

我有这个调查表格,完美无缺。我只需要改变这个问题。基本上有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>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

您的语法错误:

  1. <之前不必要的value
  2. 您的代码中没有引用id
  3. 语法错误。
  4. 使用以下内容:

    &#13;
    &#13;
    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;
      }
    }
    &#13;
    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>
    &#13;
    &#13;
    &#13;

答案 1 :(得分:0)

你有很多遗漏的代码!就像id不存在一样,函数被创建但没有初始化称为它。

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