3 回答
TA貢獻1796條經驗 獲得超7個贊
除了做JSON.parse().?.each()callback 應該應用于Arraynot an?Object,只需將您的響應對象轉換為Arrayusing?Object.values(),這是一個工作片段:
let responseStr = {2:"Robert ", 3:"Adem"}; // <----- Make sure that its an object if its not then you have to do JSON.pares().
console.log(Object.values(responseStr));
jQuery(Object.values(responseStr)).each(function(index,value){
? ?jQuery('#name').append(jQuery('<option>').val(value).text(value));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="name"></select>
TA貢獻1817條經驗 獲得超6個贊
如果來自您的 PHP 文件的響應是一個 JSON 字符串,那么您必須將它轉換成一個 javascript 數組,然后迭代它以獲得您的選擇
使用JSON.parse();
var options = JSON.parse(JSON.stringify(response));
jQuery.each( options, function( index, value ) {
$('#name').append('<option value="'+index+'">'+value+'</option>');
});
- 3 回答
- 0 關注
- 180 瀏覽
添加回答
舉報
