2013-06-12 61 views
14

我創建了一個帶有列表框和文本區域的簡單頁面,並且所有條件都是必需的。即使值爲空,textarea的「必需」屬性也不起作用

列表框工作正常,但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> 

回答

53

您有文字區域內的空白空間,將其刪除:

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

打我吧! –

+0

@ShadowWizard,哈哈,秒有問題。 :) – sinisake

+0

是的,在我之前的五秒鐘。無論如何,拿我的[小提琴](http://jsfiddle.net/GW3v9/)。 :-) –

8

問題是與標籤之間的空間。您不應該在這些標籤之間給html留出任何空格,否則瀏覽器會將其視爲值。

+0

感謝這個偉大的解決方案 – Raj

0

試試這個

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

不正確。 'required'屬性是一個布爾值。如果它存在,'textarea'將被要求填寫。因此,'required'的行爲等於'required =「required」' – Raptor

+0

正確。來自W3C和Whatwg規範的引用:「如果該屬性存在,它的值必須是空字符串或值,該屬性的規範名稱與ASCII不區分大小寫匹配,不帶前導或尾隨空格。」 https://www.w3.org/TR/html5/infrastructure.html#boolean-attributes https://html.spec.whatwg.org/multipage/infrastructure.html#boolean-attributes https:// www。 w3.org/TR/html51/infrastructure.html#sec-boolean-attributes 不過,不需要屬性值。 – hmundt

+0

正確和一些動態創建的html需求required =「required」 – user3012857

2

而且probaly形式有novalidate屬性。任何形式元素驗證屬性(如requiredregexp)與表格novalidate attribute將忽略。