首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Textarea锁定

Textarea锁定
EN

Stack Overflow用户
提问于 2016-06-15 22:28:06
回答 2查看 62关注 0票数 0

我设计了一个简单的调查表格,但由于某些原因,一行代码无法工作。

问题5.如果用户选择了单选按钮是,那么他们必须在文本区中输入一些内容,否则表单不能被发送。

如果不是,那么文本区应该被锁定,这样就不能输入任何内容。

代码是这样的:女士们先生们,谢谢你们的帮助

代码语言:javascript
复制
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>
EN

回答 2

Stack Overflow用户

发布于 2016-06-15 22:52:25

Option1:隐藏文本区域

代码语言:javascript
复制
function check5(selectedType){
  if (selectedType == 'Yes'){
    document.getElementById('5textarea').style.display = 'block'; 
	
  } else{
    
    document.getElementById('5textarea').style.display = 'none';
  
  }
}
代码语言:javascript
复制
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>

Option2:启用/禁用文本区(未测试的):

代码语言:javascript
复制
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更改为:

代码语言:javascript
复制
function check5(selectedType){
if (selectedType == 'Yes'){
  document.getElementById("5textarea").disabled = false;

} else{

  document.getElementById("5textarea").disabled = true;
}
}
票数 1
EN

Stack Overflow用户

发布于 2016-06-15 23:05:17

你必须用javascript来做这件事,因为PHP只能在服务器上运行,不能在浏览器的中运行!

在此代码中,如果不需要文本区域,则禁用该文本区域,如果选中"Yes“,并且文本区域没有包含足够的文本,则在提交表单时会出现一个错误:

代码语言:javascript
复制
// 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;
}
代码语言:javascript
复制
/* CSS: */
* {
    font-family: sans-serif;
    margin: 4px;
}
代码语言:javascript
复制
<!-- 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>

JSFiddle

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37838298

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档