คุณสมบัติ Input URL จำเป็นกำหนดว่า Input URL นั้นจำเป็นต้องตั้งค่าหรือไม่
ไวยากรณ์
ต่อไปนี้เป็นไวยากรณ์ -
- คืนค่าบูลีน - จริง/เท็จ
inputURLObject.required
- การตั้งค่า จำเป็น เป็นค่าบูลีน
inputURLObject.required = booleanValue
ค่าบูลีน
ที่นี่ “booleanValue” สามารถเป็นดังต่อไปนี้ -
| booleanValue | รายละเอียด |
|---|---|
| จริง | จำเป็นต้องตั้งค่าฟิลด์ url เพื่อส่งแบบฟอร์ม |
| เท็จ | เป็นค่าเริ่มต้นและไม่จำเป็นต้องตั้งค่าฟิลด์ url |
ตัวอย่าง
ให้เราดูตัวอย่างสำหรับ ต้องระบุ URL ที่ป้อน ทรัพย์สิน −
<!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> ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
คลิก 'ไป' ปุ่ม −

หลังจากคลิก 'ไป' ปุ่มที่มีค่าของชุดฟิลด์ url -
