gpt4 book ai didi

javascript - if 语句中最多可以使用多少个运算符?

转载 作者:行者123 更新时间:2023-11-30 07:03:28 29 4
gpt4 key购买 nike

我正在使用 jquery,我正在尝试验证表单。我的问题是 - 在单个 if 语句中最多可以进行多少次测试。

function cc_validate () {
if ($("#name").val() == "" || $("#ship_name").val() == "" || $("#address").val() == "" || $("#city").val() == "" || $("#ship_city").val() == "" || $("#state").val() == "" || $("#ship_state").val() == "" || $("#postal_code").val() == "" || isNaN($("#postal_code").val()) || $("#phone").val() == "" || $("#ship_phone").val() == "" || isNaN($("#phone").val()) || isNaN($("#ship_phone").val()) || $("#mobile_number").val() == "" || $("#ship_mobile_number").val() == "" || isNaN($("#mobile_number").val()) || isNaN($("#ship_mobile_number").val()) || $("#email").val() == "") {
return false;

} else {

return true;
} }

最佳答案

为什么,而不是你不可读的 if (并考虑稍后管理它),而不是在你的代码中添加一些类,就像这样:

<input id="postal_code" class="required number" />

让你的 jquery 像这样检查它:

function cc_validate() {
$(".required").each(function() {
if (this.val() == '') return false;
}
$(".number").each(function() {
if (isNaN(this.val()) return false;
}
}

还有这样的……?

关于javascript - if 语句中最多可以使用多少个运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1974397/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com