服務器端代碼無法檢查在javascript從JSON編碼PHP數組
$NATIVE_TEST_TEMPLATE_IDS = array(145);
客戶端代碼製備JSON來PHP數組指派到javascript對象值 -
var nativetestTemplates = "<?php echo json_encode($NATIVE_TEST_TEMPLATE_IDS); ?>"; //PHP array into JSON.
但現在,JSON對象nativetestTemplates我在這裏沒有準備好做Object對象實例檢查。我需要檢查對象nativetestTemplates中是否存在145。
以下是控制檯日誌的詳細信息 -
nativetestTemplates.length -> 5
而且,數組的單個元素具有如下 -
nativetestTemplates[0] -> [
nativetestTemplates[1] -> 1
nativetestTemplates[2] -> 4
nativetestTemplates[3] -> 5
nativetestTemplates[4] -> ]
所以,我做了什麼錯的,什麼是檢查對象中是否存在元素的最佳方法。
更新
後固定的錯誤所指出的Djave,我仍然有如果對象nativetestTemplates存在一個元問題檢查。
這裏是我的代碼 -
案例1
var testAdnetworkId = $("#adnetwork_type").val(); //reading selected dropdown value
console.log("see "+$.inArray(testAdnetworkId, nativetestTemplates)+" and "+nativetestTemplates[testAdnetworkId]);
這將返回以下,即使testAdnetworkId有匹配的值 -
see -1 and undefined
案例2
但是,如果我這樣做 -
var testAdnetworkId = 145; //hardcoded integer val
我得到.inArray $檢查所需的輸出 -
see 0 and undefined
控制檯。在殼體1 testAdnetworkId的DIR給出了這樣的 -
0 「1」
1 「4」
2 「5」
然而,在情況2中,它給 -
可以,有人請解釋在案例1中做對象屬性檢查的正確方法(讀取select下拉列表的值)
嘗試解析它? 'var nativetestTemplates = JSON.parse(json_encoded_string);' – 2014-10-08 12:33:15
在哪一步,它將如何受益?正如Djave指出的那樣,我現在糾正了這個錯誤。 – 2014-10-08 13:38:09