我有一些表单字段是从数据库动态生成的。有输入,复选框,单选按钮,文本区域和选择区域。所有字段都在ID为dynamic-form-fields
的父div中。有些字段具有required
属性。
这是一个多步骤表单,因此每个“步骤/页面”都需要先进行验证,然后才能继续进行下一步。因此,它没有提交表单。
如何检查div dynamic-form-fields
中所有必填字段是否已填写?
这是我的HTML代码的样子:
<input type="text" name="dff[]" placeholder="Name" required>
<input type="text" name="dff[]" placeholder="Date of Birth">
<input type="text" name="dff[]" placeholder="Email" required>
<input type="radio" name="gender" value="Male" required>
<input type="radio" name="gender" value="Female" required>
<select name="pet">
<option value="dog">Dog</option>
<option value="cat">Cat</option>
<option value="bird">Bird</option>
</select>
<button id="check">Check</button>
<script>
document.getElementById("check").onclick = function () {
if() {
alert('error please fill all fields!');
}
};
</script>
如您所见,某些字段是必填字段,而某些则不是必需字段。每次都不同,因此解决方案必须动态。输入的类型也不同,例如选择,单选,复选框,文本等。如何确定div ID dynamic-form-fields
中的每个“ 必需”字段都已填充?
我尝试过的事情:
也许是这样,但是如何在div ID dynamic-form-fields
下获取每种输入类型(文本,单选,复选框,选择等)?
$('#dynamic-form-fields input:required').each(function() {
if ($(this).val() === '')
alert('error please fill all fields!');
});
答案 0 :(得分:2)
document.getElementById("check").onclick = function() {
let allAreFilled = true;
document.getElementById("myForm").querySelectorAll("[required]").forEach(function(i) {
if (!allAreFilled) return;
if (!i.value) allAreFilled = false;
if (i.type === "radio") {
let radioValueCheck = false;
document.getElementById("myForm").querySelectorAll(`[name=${i.name}]`).forEach(function(r) {
if (r.checked) radioValueCheck = true;
})
allAreFilled = radioValueCheck;
}
})
if (!allAreFilled) {
alert('Fill all the fields');
}
};
<div id="myForm">
<input type="text" name="dff[]" placeholder="Name" required>
<input type="text" name="dff[]" placeholder="Date of Birth">
<input type="text" name="dff[]" placeholder="Email" required>
<input type="radio" name="gender" value="Male" required>
<input type="radio" name="gender" value="Female" required>
<select name="pet" required>
<option value="">Select a pet</option>
<option value="dog">Dog</option>
<option value="cat">Cat</option>
<option value="bird">Bird</option>
</select>
</div>
<button id="check">Check</button>
答案 1 :(得分:1)
您可以检查required
属性是否可用,
$('#dynamic-form-fields').each(function() {
var hasRequired = $(this).attr('required');
if (typeof hasRequired !== "undefined" && hasRequired !== false) {
// This is required inputs.
}
}
Ref:Link
答案 2 :(得分:1)
您可以根据输入类型进行单独检查,请考虑以下事项:
X[y==0,1] # here label is 0 so what about the 1?
X[y==1,1] # here label is 1 so what about the 1?
var isEmpty = false;
$("#check").on('click', function() {
isEmpty = false
$('input[type=text]:required').each(function() {
if ($(this).val() === '')
isEmpty = true;
});
var radioSelected = false;
//$('input[name=gender]:required').each(function() {
$('input[type=radio]:required').each(function() {
if ($(this).is(':checked'))
radioSelected = true;
});
if (!radioSelected)
isEmpty = true;
if ($("[name=pet]").val() == "")
isEmpty = true;
//if (!$(".cb").is(":checked"))
// isEmpty = true;
if (isEmpty)
alert('error please fill all fields!');
});
答案 3 :(得分:1)
您可以使用:invalid
来完成此操作。
演示:
$('#check').on("click", function(){
let valid = true;
$('[required]').each(function() {
if ($(this).is(':invalid') || !$(this).val()) valid = false;
})
if (!valid) alert("error please fill all fields!");
else alert('valid');
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" name="dff[]" placeholder="Name" required>
<input type="text" name="dff[]" placeholder="Date of Birth">
<input type="text" name="dff[]" placeholder="Email" required>
<input type="radio" name="gender" value="Male" required>
<input type="radio" name="gender" value="Female" required>
<select name="pet" required>
<option selected disabled>Select a pet</option>
<option value="dog">Dog</option>
<option value="cat">Cat</option>
<option value="bird">Bird</option>
</select>
<button id="check">Check</button>