【问题标题】:Populating Select with Jquery .ajax using json使用 json 使用 Jquery .ajax 填充 Select
【发布时间】:2015-05-30 20:11:13
【问题描述】:

努力了但没有结果 我的 Json 看起来像我用这样的 php 创建的(标题也在数据之前发送)

{"users":[
{"id":"3256","name":"Azad Kashmir"},
{"id":"3257","name":"Balochistan"},
{"id":"3258","name":"Federally Administered Tribal Areas"},
{"id":"3259","name":"Islamabad"},
{"id":"3260","name":"North-West Frontier"},
{"id":"3261","name":"Northern Areas"},
{"id":"3262","name":"Punjab"},
{"id":"3263","name":"Sindh"}], 
"results":8}

和我这样的客户端代码

<script>
$(document).ready(function(){

$("#country").change(function(){
var vc = $("#country").val();
    $.ajax({url: "../inc/region_json.php", type: "get", data:{country:vc}, dataType: 'json', success: function(json){
        $("#divx").html(json);
         $.each(json, function(i, value) {
        $('#region').append($('<option>').text(value).attr('value', value));
    }});
});

});
</script>

我想填充

region: <select name="region" id="region">
<option value="0">Choose a Country First</option>
</select><BR /><BR />

【问题讨论】:

    标签: javascript jquery html ajax json


    【解决方案1】:

    试试这个代码:-

    $(document).ready(function(){
    
    $("#country").change(function(){
        var vc = $("#country").val();
        $.ajax({
            url: "../inc/region_json.php", type: "get", data: { country: vc }, dataType: 'json', success: function (json) {
                $("#divx").html(json);
                $.each(json.users, function (i, value) {
                    $('#region').append('<option value="'+value.id+'">' + value.name + '</option>');
                });
            }
        });
       });
    
    });
    

    这里也是示例演示:- Demo

    【讨论】:

    • Gr8 代码现在正在运行...另外我必须添加 $("#region").html('');
    猜你喜欢
    • 2010-12-17
    • 1970-01-01
    • 1970-01-01
    • 2019-03-06
    • 1970-01-01
    • 2012-10-02
    • 1970-01-01
    • 2012-07-13
    • 1970-01-01
    相关资源
    最近更新 更多