Html 即使值为空,textarea 的“必需”属性也不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17061436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
textarea's "required" attribute doesn't work even though the value is empty
提问by divakar.scm
I created a simple page with list box and text area with conditions that all should be required.
我创建了一个带有列表框和文本区域的简单页面,其中包含所有应该需要的条件。
List box are working fine, but textarea box doesn't tell that the field is required to be filled.
列表框工作正常,但 textarea 框并没有告诉该字段需要填写。
<!DOCTYPE html>
<html>
<head>
<title>Ratings & Reviews</title>
<body>
<form>
<span>Customer Service*</span>
<span>
<select name=customer id=aa required>
<option selected="rate" value="" disabled>rate</option>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
<option value=5>5</option>
</select>
</span>
<br><br>
<span>Value for money*</span>
<span>
<select name=money id=aa required>
<option selected="rate" value="" disabled>rate</option>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
<option value=5>5</option>
</select>
</span>
<div>
<textarea name="reviews" rows=11 cols=50 maxlength=250 required>
</textarea>
</div>
<div id=submit>
<br>
<input type=submit name=submit value=submit>
</div>
</form>
</body>
</html>
回答by sinisake
You have empty space inside text area, remove it:
文本区域内有空白空间,请将其删除:
<textarea name="reviews" rows=11 cols=50 maxlength=250 required ></textarea>
回答by Siddharth Nagar
The problem is with the spaces between the tags. You are not supposed to give any spaces in html between these tags, otherwise browser will consider it as the value.
问题在于标签之间的空格。你不应该在这些标签之间的 html 中给出任何空格,否则浏览器会将其视为值。
回答by Mayur Morey
try this
尝试这个
<textarea name="mm" id="mm" rows="5" placeholder="NA if not applicable" required="required"></textarea>
回答by Alexander Goncharov
And probaly form has novalidate
attribute. Any form-element validation attributes (like required
or regexp
) with form novalidate attributewill ignore.
并且可能形式具有novalidate
属性。任何具有 form novalidate 属性的表单元素验证属性(如required
或regexp
)都将被忽略。
回答by code_poetry
Check default value in the textarea. There must be blank spaces which are considered as value.
检查 textarea 中的默认值。必须有空格被视为值。
回答by tim3in
I faced similar problem. I left space between opening and closing tag of Textarea as in following code
我遇到了类似的问题。我在 Textarea 的开始和结束标记之间留了空间,如下面的代码
<label><b>Register As:*</b></label>
<select name="category" id="category" class="form-control"
onchange="toggleInput()" required>
<option value=''>--Select--</option>
<option value='Attendee'>Attendee</option>
<option value='Presenter'>Presenter</option>
</select>
...
<textarea name="description" id="description" class="form-control"
placeholder="Explain here what you will present..."> </textarea>
and in my javascript I was trying following
在我的 javascript 中,我试图遵循
<script>
function toggleInput() {
if( document.getElementById("category").value=="Attendee"){
document.getElementById("description").required = false;
}
else{
document.getElementById("description").required = true;
}
}//End Function
</script>
And I could not figure out what was the problem until I landed on this page. It was the space. Thanks @sinisake for the solution. Hope sharing my experience would help someone
在我登陆此页面之前,我无法弄清楚问题出在哪里。是空间。感谢@sinisake 的解决方案。希望分享我的经验会对某人有所帮助