我有这个调查表格,它工作得很好。我只需要修改这一个问题。基本上有两个单选按钮回答"是"answers"否",并在他们下面的文本区域。现在我希望文本区域被锁定,除非用户选择"是"单选按钮,然后他们可以在文本区域输入"是"的原因。
我做了一些环顾四周并尝试这个函数,但它似乎不工作。
<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>
语法错误:
-
value
之前不需要<
- 没有
id
s在你的代码中引用。 - 语法错误。
使用如下:
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>
您有许多缺失的代码!就像id不存在一样,函数被创建但没有初始化称为its。
<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>