如何在javascript中檢查isset
。JavaScript isset()函數
我已經在以下方式使用。
var sessionvalue = document.getElementById('sessionvalue').value;
if(Here I have to check if isset the sessionvalue or not){
if(sessionvalue == "" || sessionvalue == null)
{
document.getElementById('sessionvalue').style.borderColor="red";
return false;
}
else
{
document.getElementById('sessionvalue').style.borderColor="#ccc";
}
}
,但如果' (typeof(sessionvalue.property)==「undefined」|| sessionvalue.property == null)'導致了異常'Uncaught ReferenceError:sessionvalue沒有被定義',所以只有冰山的一角才能實現甚至接近'isset'的東西。相應地投票。 – user3338098
@ user3338098問題是關於對象是否存在,而不是對象的屬性。很明顯,爲了使對象的屬性存在,對象必須首先存在。所以在這裏你的情況會稍微改變。你會檢查對象是否先存在,如果存在,繼續尋找屬性。請參閱此處的示例:https://jsfiddle.net/kx6gcLLm/ –
無論如何,它與* php *'isset'函數等效,它檢查任意數量的屬性,本質上是完全不同的。考慮:php:'isset(a-> b-> c-> d-> e-> f)'現在* javascript *'if(typeof a ==「undefined」|| a == null || typeof ab)==「undefined」|| ab == null || typeof(abc)==「undefined」|| abc == null || typeof(abcd)==「undefined」|| abcd == null || typeof (abce)==「undefined」|| abce == null || typeof(abcef)==「undefined」|| abcef == null)'現在我們可以看到差異如何極端以至於無用。 – user3338098