gpt4 book ai didi

javascript - Jquery 未定义条件不起作用

转载 作者:行者123 更新时间:2023-11-28 17:52:46 24 4
gpt4 key购买 nike

我在下面的 JavaScript 代码中为未定义设置了验证。即使该值是未定义,它也会进入if条件

if (VSATSaving.PANORAMIC_IMAGES != 'undefined' || VSATSaving.PANORAMIC_IMAGES != "") {
VSATSaving.PANORAMIC_IMAGES = lstPanaromicImages.join();
}

更新

更新了代码

var PANAROMIC_120 = $(document.getElementById('ImgPanaromic120')).data('imagename');
if (PANAROMIC_120 != "" && PANAROMIC_120 != undefined)
lstPanaromicImages.push(PANAROMIC_120);

var PANAROMIC_150 = $(document.getElementById('ImgPanaromic150')).data('imagename');
if (PANAROMIC_150 != "" && PANAROMIC_150 != undefined)
lstPanaromicImages.push(PANAROMIC_150);

var PANAROMIC_180 = $(document.getElementById('ImgPanaromic180')).data('imagename');

if (PANAROMIC_180 != "" && PANAROMIC_180 != undefined)
lstPanaromicImages.push(PANAROMIC_180);

var PANAROMIC_210 = $(document.getElementById('ImgPanaromic210')).data('imagename');
if (PANAROMIC_210 != "" && PANAROMIC_210 != undefined)
lstPanaromicImages.push(PANAROMIC_210);

var PANAROMIC_240 = $(document.getElementById('ImgPanaromic240')).data('imagename');
if (PANAROMIC_240 != "" && PANAROMIC_240 != undefined)
lstPanaromicImages.push(PANAROMIC_240);

if (VSATSaving.PANORAMIC_IMAGES != undefined || VSATSaving.PANORAMIC_IMAGES != "") {
VSATSaving.PANORAMIC_IMAGES = lstPanaromicImages.join();
}

最佳答案

您使用了OR 运算符。这意味着,如果 VSATSaving.PANORAMIC_IMAGES 值为 undefined,它仍然与 "" 不同,这就是 if 的原因code> 陈述为真。

只需将 OR 运算符替换为 AND:

if (VSATSaving.PANORAMIC_IMAGES !== undefined && VSATSaving.PANORAMIC_IMAGES !== "") {
VSATSaving.PANORAMIC_IMAGES = lstPanaromicImages.join();
}

请注意,我删除了 undefined 关键字周围的单引号。

关于javascript - Jquery 未定义条件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45103370/

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