Textarea锁定

时间:2016-06-15 14:28:06

标签: php html

我有一个我设计的简单调查表,但由于某种原因,一行代码无效。

问题5.如果用户选择单选按钮YES,则他们必须在textarea中输入内容,否则不得发送表格。

如果不是,则应锁定textarea,以便无法输入任何内容。

以下是代码:感谢女士们和女士们的帮助

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"
<?php if (isset($question5) && $question5=="Yes") echo "checked";?>
value="Yes">Yes
<br>
<input type="radio" name="question5"
<?php if (isset($question5) && $question5=="No") echo "checked";?>
value="No">No
<br>
<textarea name="question5" rows="5" cols="40"><?php echo $question5 </textarea>

2 个答案:

答案 0 :(得分:1)

选项1:隐藏textarea

&#13;
&#13;
function check5(selectedType){
  if (selectedType == 'Yes'){
    document.getElementById('5textarea').style.display = 'block'; 
	
  } else{
    
    document.getElementById('5textarea').style.display = 'none';
  
  }
}
&#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" onclick="check5(this.value)"> Yes
<br>
<input type="radio" name="question5" value="No" onclick="check5(this.value)"> No
<br>

<textarea id="5textarea" name="question5" rows="5" cols="40" style="display:none"></textarea>
&#13;
&#13;
&#13;

选项2:启用/禁用textarea( NOT TESTED ):

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" onclick="check5(this.value)"> Yes
<br>
<input type="radio" name="question5" value="No" onclick="check5(this.value)"> No
<br>
<textarea id="5textarea" name="question5" rows="5" cols="40" disabled="true"></textarea>

并将javascript更改为:

function check5(selectedType){
if (selectedType == 'Yes'){
  document.getElementById("5textarea").disabled = false;

} else{

  document.getElementById("5textarea").disabled = true;
}
}

答案 1 :(得分:0)

你必须使用javascript,因为PHP只在服务器上运行,不在浏览器中

在此代码中,如果不需要,则禁用textarea,如果&#34;是&#34;检查并且textarea没有包含足够的文本,提交表单时出现错误:

&#13;
&#13;
// Javascript:
function validate() {
    var radioYes = document.getElementById('radioYes');
    var textarea = document.getElementById('textarea');
    
    if (radioYes.checked && textarea.value.length < 10) {
        alert('Enter at least 10 characters!');
        textarea.focus();
        return false;
    }
}

function toggle(value) {
	document.getElementById('textarea').disabled = value;
}
&#13;
/* CSS: */
* {
    font-family: sans-serif;
    margin: 4px;
}
&#13;
<!-- HTML/PHP: -->
<form action="#">
    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="toggle(this.selected)" id="radioYes">Yes<br>
    <input type="radio" name="question5" value="No" onchange="toggle(!this.selected)">No<br>
    
    <textarea id="textarea" name="question5" rows="5" cols="40" disabled></textarea><br>

    <input type="submit" value="Send" onclick="return validate()" />
</form>
&#13;
&#13;
&#13;

JSFiddle