0

嗨朋友我正在尝试将我的 ajax 输出附加到我的下拉列表中......

我的ajax函数:-

 $.ajax({ 
     url: "getcolumn",
     data: {value: value},
     type: "POST",
     success: function(output) {
        var column = output;//here i am assigning the output to another variable
        var mySelect = $('#table_name');
        $.each(column, function(val, text) {
          mySelect.append($('<option></option>').val(val).html(text));
        });

我的下拉列表:-

echo $this->Form->input('Column', array(
    'label' => 'Select the column name below',
    'name' => 'tablename',
    'id' => 'table_name',
    'options' => array('null')
));

我想将 ajax 的输出附加到上面的下拉框......我试图在我的 ajax 成功函数中附加,但没有工作。谁能帮帮我....输出是json的形式.....

4

2 回答 2

0

我建议在您的 getcolumn 脚本中创建正确的 json,以便您的 jquery 每个都可以工作,json 应该如下所示:

[{id: 86,label: "venue and address"}, {id: 87,label: "venue and address"}]

然后修改一下你的循环:

mySelect.append($('<option></option>').val(text.id).text(text.label));
于 2013-02-19T11:31:46.770 回答
0

尝试这个:

$.ajax({ 
 url: "getcolumn",
 data: {value: value},
 type: "POST",
 contentType: "application/json", //<----add this
 dataType: "json"                 //<----and this
 success: function(output) {
    var column = output;//here i am assigning the output to another variable
    var mySelect = $('#table_name');
    $.each(column, function(val, text) {
      mySelect.append($('<option></option>').val(val).html(text.id));
    });
 }
 });
于 2013-02-19T11:24:57.780 回答