我将数组php转换为json,如下所示:
var jsoncsv = '<?php echo json_encode($csvData); ?>';
console.log(jsoncsv);
那些json的输出如下:
{"1":
{" NO":" 1",
"EIR IN":"1545053 ",
"CONT":"EOLU 1111111",
"TYPE":"XXXX",
"INDEPO":"21-11-2015",
"JAM":"13:00",
"KODE VSL":"ABO",
"VESSEL":"ALBERT OLDENDORFF ",
"VOY":"N001 ",
"CONSIG":"ASTABUMI CIPTA ",
"COND IN":"DMG",
"CLEAN":"DIRTY",
"TARE":" 2400",
"GROSS":" 20000",
"KAPASITAS":" 5000",
"EX CARGO":"FOOD ",
"LAST AIR)":" - - ",
"LAST HIDRO":" - - ",
"MANU":"10-11 ",
"BUILDER":" ",
"OWNER":"APL "},
"2":
{" NO":" 2",
"EIR IN":"1545052 ",
"CONT":"EOLU 1234567",
"TYPE":"IM04",
"INDEPO":"21-11-2015",
"JAM":"10:00",
"KODE VSL":"202",
"VESSEL":"WAN HAI 202 ",
"VOY":"N 001 ",
"CONSIG":"ANUGERAH AGUNG LUMIN",
"COND IN":"AVL",
"CLEAN":"DIRTY",
"TARE":" 2400",
"GROSS":" 20000",
"KAPASITAS":" 1000",
"EX CARGO":"MAKANAN ",
"LAST AIR)":" - - ",
"LAST HIDRO":" - - ",
"MANU":"11-13 ",
"BUILDER":" ",
"OWNER":"APL "}}
我选择了ON HTML:
<select data-placeholder="Masukkan EIR" id="search" >
<?php
foreach ($csvData as $v) {
echo '<option value ='. $v['NO'].' >' . $v['EIR IN'] . '- ' . $v['CONT'] . '</option>';
}
?>
</select>
使用jquery,我管理了select选项。代码看起来像这样:
$(document).on("change", '#search', function(){
var selected = $('#search').val() ;
/*Passed the selected data, search in json*/
});
假设,选择option.val
为2,如何获得json的所有“2”元素,如NO
,EIR IN
?
答案 0 :(得分:1)
这有效:
$(document).on("change", '#search', function(){
var selected = $('#search').val();
var json = <?php echo json_encode($csvData) ?>;
alert(json[selected]['EIR IN']);
});
您的HTML部分应如下所示:
<select data-placeholder="Masukkan EIR" id="search" >
<?php
foreach ($csvData as $v) {
echo '<option value ='. $v[' NO'].' >' . $v['EIR IN'] . '- ' . $v['CONT'] . '</option>';
}
?>
</select>
请注意,这里有差距 - &gt; “NO”,所以你的json中没有“NO”元素