嗯,这个问题很容易解释。我已经找了很长一段时间了,还没有找到一种正确的方法来进行这种验证。
如果所有输入为空,则只需运行一条错误消息即可。如果其中一个填好了,我就不需要停止提交表格了。
我想:
function checkForm() {
$('input').each(function(){
if( $(this).val() == "" ){
return false;
}
});
});
但是,如果至少有一个没有数据的输入,这将停止我的表单。
有什么想法吗?提前谢谢。
发布于 2018-04-30 07:30:14
反转您的逻辑,因为您希望检查一个输入字段的值是否为非空。
另外,您可能希望从实际函数返回,而不是从没有影响的回调中返回。
function checkForm() {
let bool = false;
$('input').each(function(){
if( $(this).val() !== '' ){
bool = true;
}
});
console.log(bool);
return bool;
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text">
<input type="text">
<input type="text">
<button onclick="checkForm()">check</button>
发布于 2018-04-30 07:26:52
颠倒你的逻辑。即,如果任何输入都有值,则返回true
,否则返回false
function checkForm() {
$('input').each(function() {
if ($(this).val() !== '') {
return true;
}
});
return false;
};
https://stackoverflow.com/questions/50103557
复制