javascript - 获取所有必需的输入并在页面加载时检查它们
问题描述
function showloading()
{
$('input[required="required"]').each(function(){
if( $(this).val() == "" ){
alert('Please fill all the fields');
return false;
}
});
window.scrollTo(0,0);
var x = Math.floor((Math.random() * 10) + 1);
$("#loading-"+x).show(1000);
}
我有上面的功能现在一切正常,除了线
return false;
不只是警报工作但它继续代码我想要的是检查页面是否有必填字段它必填字段为空不要运行此代码
window.scrollTo(0,0);
var x = Math.floor((Math.random() * 10) + 1);
$("#loading-"+x).show(1000);
谢谢
解决方案
这是纯javascript中的实现
function showloading() {
// get a NodeList of all required inputs, and destructure it into an array
const required = [...document.querySelectorAll('input[required]')];
// Use Array.prototype.some() to find if any of those inputs is emtpy
// and if so, return false (exiting showLoading)
if (required.some(input => input.value === '')) {
alert('Please fill all the fields');
return false;
}
/* whatever you want to do if all required fields are non-empty */
}
推荐阅读
- javascript - Express res.write 不等待一系列承诺完成
- ios - Swift - 制作 CAShapeLayer 发光效果
- javascript - 'CI' 不是内部或外部命令、可运行程序或批处理文件
- unix - 我想在两个路径之一中找到一个具有特定名称的文件。第二个包含一个未知文件夹
- scheduled-tasks - 使用 freeRTOS 通过 systick_Handler 设置 1ms 中断,导致任务只运行一次
- r - 从 2 个分类变量计算新变量
- excel - 如何从日期范围更改 Excel 中的年份值?
- javascript - 在找不到标题导航栏文件中包含外部 javascript 文件
- r - 在 tidyverse 的每一列中计算它们后按降序排列汇总的 NA
- reactjs - React-native 和 redux 结合从 firestore 获取的动作