Jquery数组.如何同时显示与if语句匹配的数组元素
Jquery Array. How to display array elements that match if statements at once?
这是我的脚本:
<script>
$('.change_password_button').click(function() {
var error = [];
if (!$('.password').val()) {
error[0] = "Current password field is empty.";
}
if (!$('.new_password').val()) {
error[1] = "New password field is empty.";
}
if (!$('.confirm_password').val()) {
error[2] = "Confirm password field is empty.";
}
if ($('.new_password').val() != $('.confirm_password').val()) {
error[3] = "Your new password and confirm password fields do not match.";
}
for (var i = 0; i < error.length; i = i + 1) {
$('#errors').show();
$('#errors').html(error[i]);
}
});
</script>
我想同时显示所有错误,但现在只显示一条错误消息。提前感谢您的回答。
您有多个问题。
问题1:首先,如果没有错误,索引0会发生什么?它是未定义的。
解决方案:使用推送,不要设置索引。
问题2:第二,您只是在循环中设置innerHTML,以便不断覆盖它。
解决方案:加入阵列
问题3:您的价值()检查将不起作用,
解决方案:您需要检查的长度
$('.change_password_button').click(function(){
var error = [];
if (!$('.password').val().length) {
error.push("Current password field is empty.");
};
if (!$('.new_password').val().length) {
error.push("New password field is empty.");
};
if (!$('.confirm_password').val().length) {
error.push("Confirm password field is empty.");
};
if ($('.new_password').val() != $('.confirm_password').val()) {
error.push("Your new password and confirm password fields do not match.");
};
if(error.length) {
$('#errors').html( error.join("<br/>").show();
} else {
$('#errors').hide();
}
}
尝试error.join('')
,而不是迭代和更新元素
$('.change_password_button').click(function () {
var error = [];
if (!$('.password').val()) {
error.push("Current password field is empty.");
};
if (!$('.new_password').val()) {
error.push("New password field is empty.");
};
if (!$('.confirm_password').val()) {
error.push("Confirm password field is empty.");
};
if ($('.new_password').val() != $('.confirm_password').val()) {
error.push("Your new password and confirm password fields do not match.");
};
$('#errors').show();
$('#errors').html(error.join(''));
});
如果你想使用循环,那么附加html,而不是覆盖它
var $errors = $('#errors').empty()
for (var i = 0; i < error.length; i = i + 1) {
$errors.append(error[i]);
}
$errors.show();
与其每次都重写HTML,不如开始添加:
var current = $('#errors').html();
$('#errors').html( current + " " + error[ i ] );
附加一个错误的ul
可能更合适,但这会让你开始。
要回答这个问题:您为每个错误覆盖#errors
元素的HTML,因此它最终只显示最后一个错误。您需要将每个错误消息附加到前一个错误消息。
您可以按照tymeJV的建议进行操作,但这需要每次运行循环时获取该div的HTML。jQuery已经提供了开箱即用的附加功能,为什么不使用它呢?jQuery团队为此付出了很多努力
...
$('#errors').show(); // Notice I moved this statement. Once it is shown, you do not need to show it again.
for (var i = 0; i < error.length; i = i + 1) {
$('#errors').append(error[i]); // .append() instead of .html().
}
...
注意.html():
当使用.html()设置元素的内容时,该元素中的任何内容都将被新内容完全替换
因此,您总是将#errors的内容从error[0]替换为error[length-1],任何时候都只使用一条错误消息。
我建议使用.append():
.append()方法将指定的内容作为jQuery集合中每个元素的最后一个子元素插入
相关文章:
- 在函数中添加数组元素的数值
- 访问JSON对象内部的数组元素
- Mongoose-在更新中删除数组元素
- javascript数组元素是否知道其封闭数组
- 将数组元素附加到FormData dos'不适用于Firefox 15
- 如何在javascript中使用click函数选择数组元素
- 如何在JavaScript中剥离数组元素中的非整数
- 消隐数组元素是否生成自己的属性
- 如何使一个Math.random数组元素比另一个数组元素更有可能被选中
- 在Codrops的内容中添加数组元素展开缩略图网格预览
- 如何使用Jquery水平打印表中的数组元素,并在某个元素之后垂直打印
- 如何访问数组中的数组元素(JavaScript)
- 生成ACF标记位置的数组(元素列表后缺少])
- validate.js验证数组元素
- JavaScript Unshift EACH 数组元素
- 如何在加号运算符之后选择数组元素的一部分
- 访问标记图标的图像数组元素
- JavaScript 合并相同的数组元素
- 删除仅空格数组元素
- Jquery数组.如何同时显示与if语句匹配的数组元素