简体   繁体   English

javascript检查对象存在

[英]javascript check object exists

I have following code to check the object whether exists for item listing. 我有以下代码来检查对象是否存在项目列表。 For testing purpose, i only have one object sub_total_0 , however the script keep looping because typeof cannot determine sub_total_1 is undefined or not exists, and keep going 2,3,4,5,... 出于测试目的,我只有一个对象sub_total_0 ,但是脚本保持循环,因为typeof无法确定sub_total_1是未定义的还是不存在,并继续2,3,4,5,...

var i = 0;
while (typeof document.getElementById("sub_total_" + i) != "undefined") {
    var obj_sub_total = document.getElementById("sub_total_" + i);
    if (obj_sub_total != "undefined") {
        if (fr.order_method_id.value == 1) {
            obj_sub_total.style.visibility = "visible";
        } else {
            obj_sub_total.style.visibility = "hidden";
        }
    }
    i++;
} 

You have 你有

typeof document.getElementById("sub_total_" + i) != "undefined"

and

if (obj_sub_total != "undefined") {

getElementById returns either null or an HTML element. getElementById返回null或HTML元素。 Neither of these are the string "undefined" and the type of each of this will be "object" . 这些都不是字符串"undefined"并且每个字符串的类型都是"object" Thus your conditions don't make sense. 因此,你的条件没有意义。

You test for truthfulness instead. 你测试的是真实性。 An HTML element will always be true and null will always be false. HTML元素始终为true, null始终为false。

while (document.getElementById("sub_total_" + i)) {

and

if (obj_sub_total) {

getElementById() return null if element is not found, and the type of null is object that is why your condition is not working. 如果找不到元素,则getElementById()返回null,并且null的类型是对象,这就是您的条件不起作用的原因。

You can just check whether it is a truthy value and since the while() loop validates the object there is no need for the if condition 你可以检查它是否是一个真值,并且因为while()循环验证了对象,所以不需要if条件

var i = 0,
    obj_sub_total;
while (obj_sub_total = document.getElementById("sub_total_" + i)) {
    console.log(obj_sub_total)
    if (fr.order_method_id.value == 1) {
        obj_sub_total.style.visibility = "visble";
    } else {
        obj_sub_total.style.visibility = "hidden";
    }
    i++;
}

Demo: Fiddle 演示: 小提琴

Your check doesn't work because using typeof on getElementById 's return value will always give you "object" , because it returns null if it can't find it, and typeof null is "object" . 您的检查不起作用,因为在getElementById的返回值上使用typeof始终为您提供"object" ,因为如果找不到它则返回null ,而typeof null"object"

Just check the return value directly: If there's an element, it's a "truthy" value (one that coerces to true when used as a condition); 只需直接检查返回值:如果有一个元素,则它是一个“真实”值(当用作条件时强制为true ); if there isn't one, null is a "falsey" value (coerces to false ). 如果没有,则null为“falsey”值(强制为false )。

So: 所以:

while (document.getElementById("sub_total_" + i)) {

You also don't need to look it up twice, which is what you're currently doing; 你也不需要两次查找,这就是你现在正在做的事情; instead: 代替:

var obj_sub_total;
while ((obj_sub_total = document.getElementById("sub_total_" + i)) != null) {

(You don't technically need the != null there, but without it it looks a bit like you've accidentally used = where you wanted == .) (你在技术上不需要!= null ,但没有它看起来有点像你不小心使用=你想要的地方== 。)


Another alternative would be to use a class and querySelectorAll : 另一种方法是使用类和querySelectorAll

var list = document.querySelectorAll(".sub_total");

Then loop while i is < list.length , eg: 然后循环,而i< list.length ,例如:

var list = document.querySelectorAll(".sub_total");
for (i = 0; i < list.length; ++i) {
    var obj_sub_total = list[i];
    // ...
}

Or you could do that even when using id s: 或者即使使用id s你也可以这样做:

var list = document.querySelectorAll("[id^=sub_total]");

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

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