フォームの入力値を収集し、localstorage を使用してそれらを保存しています。
入力の 1 つをチェックし、必要な処理を実行するコードを次に示します。ただし、入力は 1 つだけです。
$('#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
実行されたときにのみチェックすることを好みます。そのため、ユーザーには「ナグ」が 1 つしかありません。
あなたの助けのための初心者からのありがとう!
更新: .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';
}
});
});