大家好,試圖讓我的復選框在提交我的按鈕之前需要點擊,這是一個 onclick 不提交?
<form>
<p>
<input style="padding:14px; -webkit-border-radius: 30px; -moz-border-radius: 30px; border-radius: 30px; width: 300px; border: none;" placeholder="Enter Date Here... e.g 17/05/1981" />
</p>
<p>
<input type="checkbox" id="vehicle3" name="vehicle3" value="Boat" required="true"> <label style="color: #fff; font-size: 10px;"> Please Accept Terms</label></p>
<p><a id="generateButton" href="generate.html" class="progress-button red" data-loading="Creating..." data-finished="Start Over" data-type="background-vertical" onclick="getRandomImage()">Start Search</a></p>
</form>
uj5u.com熱心網友回復:
您可以向 clickHandler 添加邏輯以檢查該框是否已選中。因此,不是getRandomImage()直接呼叫,而是添加一些邏輯來有條件地呼叫函式。
function clickHandler = () => {
// get the checkbox element from the DOM
const checkboxElement = document.getElementById('vehicle3');
// see if the checkbox is checked
if (checkBoxElement.checked) {
getRandomImage();
} else {
console.log('The checkbox wasn't checked!');
}
}
// add the handler to the button
document.getElementById('generateButton').addEventListener('click', clickHandler);
uj5u.com熱心網友回復:
不幸的是,HTML5 沒有提供開箱即用的方式來做到這一點。
但是,使用 jQuery 或 javascript,您可以輕松控制復選框組是否至少有一個選中的元素。
function myFunction() {
document.getElementById("myCheck").required = true;
}
<form action="/action_page.php">
Checkbox: <input type="checkbox" id="myCheck" name="test">
<input type="submit" onclick="myFunction()">
</form>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/334585.html
標籤:javascript html 按钮 复选框 提交
