HTML DOM 輸入 URL required 屬性
Input URL required 屬性確定是否必須設定 Input URL。
語法
以下是語法 −
- 返回布林值 - true/false
inputURLObject.required
- 將 required 設定為 booleanValue
inputURLObject.required = booleanValue
布林值
此處,“booleanValue”可以是以下選項 −
| booleanValue | 詳細資訊 |
|---|---|
| True | 必須設定 url 欄位才能提交表單。 |
| False | 這是預設值,不必設定 url 欄位。 |
示例
讓我們看一個 Input URL required 屬性的示例 −
<!DOCTYPE html>
<html>
<head>
<title>Input URL required</title>
<style>
form {
width:70%;
margin: 0 auto;
text-align: center;
}
* {
padding: 2px;
margin:5px;
}
input[type="button"] {
border-radius: 10px;
}
</style>
</head>
<body>
<form>
<fieldset>
<legend>URL-required</legend>
<label for="URLSelect">URL:
<input type="url" id="URLSelect" required>
</label><br>
<input type="button" onclick="showMessage()" value="Go">
<div id="divDisplay"></div>
</fieldset>
</form>
<script>
var divDisplay = document.getElementById("divDisplay");
var inputURL = document.getElementById("URLSelect");
function showMessage() {
if(inputURL.required === true && inputURL.value === '')
divDisplay.textContent = 'Cannot Redirect: URL is Required';
else
divDisplay.textContent = 'Redirecting...';
}
</script>
</body>
</html>輸出
這將產生以下輸出 −
單擊 ‘Go’ 按鈕 −

單擊 ‘Go’ 按鈕並設定 url 欄位的值後 −

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP