HTML DOM 輸入搜尋所需的屬性
HTML DOM 輸入搜尋所需的屬性與 <input> 元素的 required 特性相關聯。required 特性用於設定和返回在將表單提交給伺服器之前填充某些搜尋欄位是否必要。這將允許使用者放空具有 required 特性的搜尋欄位後不提交表單。
語法
以下是語法 −
設定 required 特性 −
searchObject.required = true|false
這裡,true 表示必須填寫搜尋欄位,而 false 表示在提交表單之前可以不填寫該欄位。
示例
讓我們看一個關於輸入搜尋所需屬性的示例 −
<!DOCTYPE html>
<html>
<body>
<h1>Input search required property</h1>
<p>Check if the above field is mandatory to be filled or not by clicking the below
button</p>
<form action="/Sample_page.php">
FRUITS: <input type="search" id="SEARCH1" name="fruits" required>
<input type="submit">
</form>
<br>
<button onclick="checkReq()">CHECK</button>
<p id="Sample"></p>
<script>
function checkReq() {
var Req=document.getElementById("SEARCH1").required;
if(Req==true)
document.getElementById("Sample").innerHTML="The search field must be filled before submitting";
else
document.getElementById("Sample").innerHTML="The search field is optional and can be left blank by the user";
}
</script>
</body>
</html>輸出
這將產生以下輸出 −

點選 CHECK 按鈕時 −

如果 required 特性設定為 true 並且你點選提交 −

廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP