即使值为空,textarea的“required”属性也不起作用

问题描述 投票:20回答:5

我创建了一个带有列表框和文本区域的简单页面,其中包含所有条件都需要的条件。

列表框工作正常,但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>
html html5 textarea required
5个回答
67
投票

文本区域内有空格,将其删除:

 <textarea name="reviews" rows=11 cols=50 maxlength=250 required ></textarea>

Fiddle demo


10
投票

问题在于标签之间的空格。您不应该在这些标记之间的html中给出任何空格,否则浏览器会将其视为值。


3
投票

试试这个

<textarea name="mm" id="mm" rows="5" placeholder="NA if not applicable" required="required"></textarea>

2
投票

而probaly形式有novalidate属性。形式为required的任何表单元素验证属性(如regexpnovalidate attribute)都将忽略。


0
投票

检查textarea中的默认值。必须有空格被视为值。


0
投票

我遇到了类似的问题。我在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>

并在我的JavaScript中我试图跟随

<script>
   function toggleInput() {  
      if( document.getElementById("category").value=="Attendee"){  
        document.getElementById("description").required = false;
      }
      else{
        document.getElementById("description").required = true;
      }
   }//End Function
</script>

在我登陆这个页面之前,我无法弄清楚问题是什么。这是空间。谢谢@sinisake的解决方案。希望分享我的经验会有所帮助

© www.soinside.com 2019 - 2024. All rights reserved.