我正在收集表单输入值并使用本地存储来保存它们。
这是检查其中一个输入并执行我想要的操作的代码 - 但仅针对一个输入:
$('#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
时检查,这样用户就只有一个“nag”。
感谢新手的帮助!
更新: .each
被建议,但怀疑一定有比这更好的方法:
$('#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() 迭代每个元素:)
关于jquery - 我将如何扩展此输入验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11602814/