2017-01-26 17 views
0

我試圖從數據標記獲取數組值到jQuery但不工作。 這裏是我的代碼:從數據標記到jQuery的php數組值向我致謝

//$b is taking names from text-area separated by comma. 
$a = array_map('trim', explode(",", $b)); 
return <input type="text" id="pct"><button id="pcthit">submit</button><p id="pct_avail"></p></span> 
<input type="hidden" id="storageElement" data-storeIt="'.$a.'">'; 


$("#pcthit").click(function(){ 
    var pstcde = $("#pct").val(); 
    var retrieved_string = []; 
    var retrieved_string = $("#storageElement").data('storeit'); 
    if((pstcde != '') && (jQuery.inArray(pstcde, retrieved_string) >= 0)){ 
    $("#pct_avail").html('hello'); 
    } 
}); 

回答

1

您需要的陣列json_encode()編碼。

return '<input type="text" id="pct"><button id="pcthit">submit</button> 
     <p id="pct_avail"></p></span> 
     <input type="hidden" id="storageElement" data-storeIt=\''.json_encode($a).'\'>'; 
+0

謝謝Barmar,我試過用json_encode(),但仍然無法正常工作。 $( 「#pct_avail」)HTML(retrieved_string);只給出括號「[」。 – 010101010101

+0

我用(')和(')替換了所有的輸入引號,(「),現在它正在工作。非常感謝您的幫助:), – 010101010101

+0

我剛剛更改'data-storeIt'值的引號,因爲'json_encode()'在所有嵌入的字符串周圍使用了雙引號。 – Barmar