开发者

How can I tell 'Whether a Javascript object exists on my page'?

I can't get the the conditional to work in this function, any help?

function headupdate(id, name, heading)
{
    var order;
    if (document.getElementById(heading).value === undefined)
    {
        order = 1;
    }
    else
    {
        order = document.getElementById(heading).value;
    }
    alert(order);
    $.post('headingupdate.php', {id: id, name: name, value: heading, order: order},
        function(response)
        开发者_Go百科{
            $('#resume').html(response)
        }
    )
};


You should check as following.

var head = document.getElementById(heading);
if(head!=null)
{
  order = head.value;
}
else
{
order=1;
}


In response to your post title, I use typeof() to find if an element exists in the DOM:

if(typeof(document.getElementById('someElement')=='undefined')) {
  alert('Element DNE');
}

Also, typeof() returns a string, so it needs to be in quotes for a conditional statement

"undefined"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜