gpt4 book ai didi

javascript - 如何将 json 值附加到选择列表

转载 作者:行者123 更新时间:2023-11-30 20:41:28 25 4
gpt4 key购买 nike

[{"key":"0.75","value":"3/4","Default_Value":"0"}, 
{"key":"0.875","value":"7/8","Default_Value":"1"},
"key":"1.75","value":"1 3/4","Default_Value":"0"}]

我有 json 数组如何将这个 json 有选项附加到选择列表

这里是我想追加的方式

<option value="0.75">3/4</option>
<option value="0.875">7/8</option>
<option value="1.75">1 3/4</option>

如果“Default_Value”:“1”我需要选择默认值我不知道如何迭代和获取值以及如何在选项中显示关键值的痛苦,非常感谢任何帮助。

$("#p_combination").empty();
//supposing that your response object is something like :
var response=[{'dish_name':'3/4','dis-val':0.75},{'dish_name':'7/8','dis-val':0.875},{'dish_name':'1.75','dis-val':1.75}];
for(var i = 0; i < response.length; i++) {
$("#p_combination").append("<option value='" + response[i]['dis-val'] + "'>" +response[i]['dish_name'] + "</option>");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<select name="p_combination[]" id="p_combination" multiple>
<option value="0">Select</option>
</select>

最佳答案

 $arrays=json_decode([{"key":"0.75","value":"3/4","Default_Value":"0"}, 
{"key":"0.875","value":"7/8","Default_Value":"1"},
"key":"1.75","value":"1 3/4","Default_Value":"0"}]);
foreach($arrays as $array){
$select="";
if($array->Default_Value==1){
$select="selected='selected'";
}
echo '<option '.$select.' value="'.$array->value.'">'.$array->value.'</option>';
}

关于javascript - 如何将 json 值附加到选择列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49206343/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com