Jquery 如何扩展此输入验证?
我正在收集表单输入值并使用localstorage保存它们 下面是检查其中一个输入并执行我想要的操作(但仅针对一个输入)的代码:Jquery 如何扩展此输入验证?,jquery,Jquery,我正在收集表单输入值并使用localstorage保存它们 下面是检查其中一个输入并执行我想要的操作(但仅针对一个输入)的代码: $('#refresh').click(function() { if (localStorage['t'] == null || localStorage['t'].length == 0) { $("input#t").css("background-color", "yellow"); return; }
$('#refresh').click(function() {
if (localStorage['t'] == null || localStorage['t'].length == 0) {
$("input#t").css("background-color", "yellow");
return;
}
save_data()
window.location = 't4.html';
});
因此,我将有更多的输入值(t、t1、t2…),并希望突出显示示例中执行的任何/所有空值。验证插件似乎没有必要,因为这是唯一需要验证的地方。另外,我不希望在输入条目时进行检查,而只希望在执行#刷新时进行检查
谢谢新手对你的帮助
更新:。每个
都是建议的,但怀疑一定有比这更好的方法:
$('#refresh').click(function () {
$("input").each(function (i) {
if(localStorage['t'] == null || localStorage['t'].length == 0 ) {
$("input#t").css("background-color","yellow");
return;
}
if(localStorage['t1'] == null || localStorage['t1'].length == 0 ) {
$("input#t1").css("background-color","yellow");
return;
}
if(localStorage['t2'] == null || localStorage['t2'].length == 0 ) {
$("input#t2").css("background-color","yellow");
return;
}
if(localStorage['t3'] == null || localStorage['t3'].length == 0 ) {
$("input#t3").css("background-color","yellow");
return;
}
else {
save_data()
window.location = 't4.html';
}
});
});
可以使用jQuery的.each():)迭代每个元素
验证插件可以节省工作并使您的验证更加可靠,为什么要避免?@Kos为一个验证实例加载一个功能齐全的验证脚本似乎有些过分。另外,我看到的验证插件似乎是设计用来在输入时检查单个输入的,而不是我上面提到的一次性“nag”。您打算如何为.each声明localstorage变量?