简体   繁体   English

检查多个条件值不会输出else语句

[英]Checking multiple condition values does not output the else statement

I am attempting to check a list of zip codes and then assign the variable zipAssign a value based on if the zip code entered matches one from the list. 我正在尝试检查邮政编码列表,然后根据输入的邮政编码是否与列表中的邮政编码匹配,为变量zipAssign分配一个值。

In the codes current state it always displays "AB". 在代码当前状态下,它始终显示“AB”。

Does anyone see what I am doing wrong? 有谁看到我做错了什么?

 $('#salesforce_submit').change(function () { //Find AB Zip Code var zipVal = ''; var zipAssign = ''; zipVal = $('#zip').val(); if (zipVal = 44030 || 44048 || 44082 || 44003 || 44093 || 44076 || 44062 || 44021 || 44046 || 44099 || 44032 || 44047 || 44010 || 44057 || 44086 || 44064 || 44024 || 44023 || 44065 || 44022 || 44072 || 44040 || 44143 || 44094 || 44139 || 44146 || 44128 || 44105 || 44122 || 44124 || 44121 || 44117 || 44108 || 44110 || 44103 || 44106 || 44118 || 44120 || 44104 || 44114 || 44127 || 44125 || 44131 || 44134 || 44129 || 44130 || 44144 || 44109 || 44115 || 44136 || 44133 || 44147 || 44141 || 44067 || 44056 || 44087 || 44195) { zipAssign = 'AB'; } else { zipAssign = ''; } console.log(zipAssign); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <form id="salesforce_submit" method="POST" enctype="multipart/form-data"> <div><input id="zip" placeholder="Zip/Postal Code*" class="input block" maxlength="6" name="zip" type="text" pattern= "[0-9]{5}"></div> <input type="submit"> </form> 

In the codes current state it always displays "AB". 在代码当前状态下,它始终显示“AB”。

Does anyone see what I am doing wrong? 有谁看到我做错了什么?

because zipVal = 44030 || 44048 因为zipVal = 44030 || 44048 zipVal = 44030 || 44048 will always return a truthy value. zipVal = 44030 || 44048将始终返回真值。

Create an array of zipVals and use includes 创建一个zipVals数组并使用includes

var zipVals = [ 44030 , 44048 , 44082 , 44003 , 44093 , 44076 , 44062 , 44021 , 44046 , 44099 , 44032 , 44047 , 44010 , 44057 , 44086 , 44064 , 44024 , 44023 , 44065 , 44022 , 44072 , 44040 , 44143 , 44094 , 44139 , 44146 , 44128 , 44105 , 44122 , 44124 , 44121 , 44117 , 44108 , 44110 , 44103 , 44106 , 44118 , 44120 , 44104 , 44114 , 44127 , 44125 , 44131 , 44134 , 44129 , 44130 , 44144 , 44109 , 44115 , 44136 , 44133 , 44147 , 44141 , 44067 , 44056 , 44087 , 44195 ];

    if ( zipVals.includes( +zipVal ) ) {    //convert zipVal to a number before using includes
        zipAssign = 'AB';
    } else {
        zipAssign = '';
    }

Unfortunately, you can't do that. 不幸的是,你做不到。

This is what you are looking for: if (zipVal === 44030 || zipVal === 44048 || etc...) 这就是你要找的: if (zipVal === 44030 || zipVal === 44048 || etc...)

However, another option is to create an array of all the zipCodes and then check your condition with if (myArray.includes(zipVal)) 但是,另一种选择是创建所有zipcode的数组,然后用if (myArray.includes(zipVal))检查你的条件

  1. When checking equality, you need to write == or === instead of = : zipVal == 44030 . 检查相等性时,需要编写=====而不是=zipVal == 44030

  2. When you want to compare the same variable against multiple values, you still have to write out all the conditions: zipVal == 44030 || zipVal == 44048 || (etc.) 当您想要将同一个变量与多个值进行比较时,您仍然必须写出所有条件: zipVal == 44030 || zipVal == 44048 || (etc.) zipVal == 44030 || zipVal == 44048 || (etc.)

Note however that this is not a good way to check if a value is in a set of values this large. 但请注意,这不是检查值是否在一组值中的好方法。 Instead, you should collect the ZIP codes in an array: 相反,您应该收集数组中的邮政编码:

var zipCodes = [ 44030, 44084, (etc.) ]

And then just use .includes() : 然后使用.includes()

if (zipCodes.includes(zipVal)) {
    zipAssign = 'AB';
}

you just made a small mistake, instead of comparing (==) two value, you assigned one to another (=) in the if condition. 你只是犯了一个小错误,而不是比较(==)两个值,你在if条件中分配了一个到另一个(=)。

Here I recreated you code. 在这里,我重新创建了代码。

 $('#salesforce_submit').change(function () { //Find AB Zip Code var zipVal = ''; var zipAssign = ''; zipVal = $('#zip').val(); if (zipVal == 44030 || zipVal == 44048 || zipVal == 44082 || zipVal == 44003 || zipVal == 44093 || zipVal == 44076 || zipVal == 44062 || zipVal == 44021 || zipVal == 44046 || zipVal == 44099 || zipVal == 44032 || zipVal == 44047 || zipVal == 44010 || zipVal == 44057 || zipVal == 44086 || zipVal == 44064 || 44024 || 44023 || 44065 || 44022 || 44072 || 44040 || 44143 || 44094 || zipVal == 44139 || zipVal == 44146 || zipVal == 44128 || zipVal == 44105 || zipVal == 44122 || zipVal == 44124 || zipVal == 44121 || zipVal == 44117 || zipVal == 44108 || zipVal == 44110 || zipVal == 44103 || zipVal == 44106 || zipVal == 44118 || zipVal == 44120 || zipVal == 44104 || zipVal == 44114 || zipVal == 44127 || zipVal == 44125 || zipVal == 44131 || zipVal == 44134 || zipVal == 44129 || zipVal == 44130 || zipVal == 44144 || zipVal == 44109 || zipVal == 44115 || zipVal == 44136 || zipVal == 44133 || zipVal == 44147 || zipVal == 44141 || zipVal == 44067 || zipVal == 44056 || zipVal == 44087 || zipVal == 44195) { zipAssign = 'AB'; } else { zipAssign = ''; } console.log(zipAssign); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <form id="salesforce_submit" method="POST" enctype="multipart/form-data"> <div><input id="zip" placeholder="Zip/Postal Code*" class="input block" maxlength="6" name="zip" type="text" pattern= "[0-9]{5}"></div> <input type="submit"> </form> 

But I strongly suggest to create an array of ZIP code and loop it with the input value for comparing. 但我强烈建议创建一个邮政编码数组并将其与输入值一起循环以进行比较。

I hope this will help you. 我希望这能帮到您。 Thank you. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM