Javascript 多个或多个字符串
我无法找到如何在If语句中传递多个字符串 这是我的密码:Javascript 多个或多个字符串,javascript,string,if-statement,return-value,Javascript,String,If Statement,Return Value,我无法找到如何在If语句中传递多个字符串 这是我的密码: var date = new Date(); if (document.getElementById("postcode-entry").value == ("G74" || "G75")) { if (date.getHours() < 8 ) { window.alert("Sorry we are not open at the moment, please try a
var date = new Date();
if (document.getElementById("postcode-entry").value == ("G74" || "G75")) {
if (date.getHours() < 8 ) {
window.alert("Sorry we are not open at the moment, please try again later.");
} else {
window.open("http://http://stackoverflow.com");
}
} else {
window.alert("Sorry we do not Delivery to your area, please collect from store");
}
var-date=新日期();
if(document.getElementById(“邮政编码条目”).value==(“G74”| |“G75”)){
if(date.getHours()<8){
window.alert(“很抱歉,我们目前没有打开,请稍后再试。”);
}否则{
窗口打开(“http://http://stackoverflow.com");
}
}否则{
提醒(“对不起,我们没有送货到您的地区,请从商店领取”);
}
我怎样才能做到这一点?这应该可以
var post_code = document.getElementById("postcode-entry").value;
if (post_code == "G74" || post_code == "G75")
这应该可以
var post_code = document.getElementById("postcode-entry").value;
if (post_code == "G74" || post_code == "G75")
短语(“G74”| |“G75”)
强制对每个字符串进行布尔运算,并且两者都将始终返回true
所以你需要这样做:
var myvar = document.getElementById("postcode-entry").value;
if(myvar === "G74" || myvar === "G75")
短语(“G74”| |“G75”)
强制对每个字符串进行布尔运算,并且两者都将始终返回true
所以你需要这样做:
var myvar = document.getElementById("postcode-entry").value;
if(myvar === "G74" || myvar === "G75")
我以前从未见过这种情况。也许您可以使用switch语句 但就你的情况而言,我建议如下:
var poscode = document.getElementById("postcode-entry").value
if (postcode === "G74" || postcode === "G75") ......
我以前从未见过这种情况。也许您可以使用switch语句 但就你的情况而言,我建议如下:
var poscode = document.getElementById("postcode-entry").value
if (postcode === "G74" || postcode === "G75") ......
我不确定您是否希望采用这种方法,但请尝试使用以下方法-
var strArr = [ 'G74', 'G75' ];
if( strArr.indexOf( document.getElementById("postcode-entry").value ) !== -1 ) {
// Your normal code goes here
}
使用此方法,您可以在if中的单个语句中测试n个字符串。我不确定您是否希望采用此方法,但请尝试使用以下方法-
var strArr = [ 'G74', 'G75' ];
if( strArr.indexOf( document.getElementById("postcode-entry").value ) !== -1 ) {
// Your normal code goes here
}
使用它,您可以在if中的一条语句中测试n个字符串。谢谢Faust,这是一个巨大的帮助!谢谢浮士德,这么大的帮助!