gpt4 book ai didi

javascript - Codeigniter Jquery Ajax : How to loop returned data as html

转载 作者:行者123 更新时间:2023-12-02 16:48:29 25 4
gpt4 key购买 nike

我对 JQuery AJAX 不太熟悉,这是我的脚本:

$(document).ready(function() {
$("#city").change(function() {

var city_id = $("#city").val();

if (city_id != '') {
$.ajax({
type: "POST",
url: "<?php echo base_url() ?>index.php/home/get_block_by_id/" + city_id,
success: function(block_list) {
// WHAT TO PUT HERE ?
},
});
}
});

如果我输入 console.log(block_list) 它会返回 JSON 类型的正确数据:

[{"id":"1601","id_city":"16","block":"A"},
{"id":"1602","id_city":"16","block":"B"}]

循环返回数据的正确方法是什么?我这样做是为了看看循环返回了什么:

$.each(block_list, function() {
$.each(this, function(index, val) {
console.log(index + '=' + val);
});
});

但它完全搞砸了:(,如果循环数据是正确的,我还想将id作为valueblock name作为text作为我的<option>标签怎么办那?谢谢。

更新

抱歉,我已经尝试了两个答案,但它不起作用,我尝试将代码更改为:

$("#city").change(function(){

var city_id = $("#city").val();

$.get("<?php echo base_url() ?>index.php/home/get_block_by_id/" + city_id, function(data) {
$.each(data, function(id, val) {
console.log(val.id);
});
});

});

它返回:

**UNDEFINED**

我也尝试改成val[id]val['id']仍然无法正常工作,请帮助:(

最佳答案

$.each(block_list, function(id, block){
console.log('<option value="' + block['id'] + '">' + block['block'] + '</option>')
});

输出将是:

<option value="1601">A</option>
<option value="1602">B</option>

关于javascript - Codeigniter Jquery Ajax : How to loop returned data as html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26905577/

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