dtwknzk3764 2014-05-26 16:06
浏览 60
已采纳

Jquery Ajax调用php脚本我该如何处理数据?

I am trying to get this ajax call to work:

<script type="text/javascript">
$( document ).ready(function() {

        $('#home_location').change(function(){
                $.ajax({
                        url: 'GetCommunities.php',
                        data: {id: $(this).val()},
                        datatype: 'json',
                        success: function(data){
                                console.log(data);
                        }
                });
        });

});
</script>

The console log returns my data like so:

[{"rb_communityId":"8","rb_communityLabel":"Sunflower","rb_communityOrder":"12","rb_locationId":"4"}] 

my question is how do I make the rb_communitiyId into an array and use that array to for select options to replace a current dropdown?

  • 写回答

4条回答 默认 最新

  • doumou3883 2014-05-26 16:20
    关注

    this code:

    $.ajax({
       url: 'GetCommunities.php',
       data: {id: $(this).val()},
       datatype: 'json',
       success: function(data){
          console.log(data);
       });
    });
    

    will NOT result to a JSON object, but in a string! replace datatype to dataType and be sure to call echo json_encode(your_array); exit; in your php file.

    Normally, you would be able to access your data (in success) like data.rb_communityId and about changing a dropdown, just use this: (Replace #mydropdown with your own id)

    $("#mydropdown").val( data.rb_communityId );
    

    assuming the .communityId is a value of your dropdown.

    If you want to replace a dropdown entirely based on the data, do this (in success again):

    $("#mydropdown option").remove();
    $.each(data,function(key,value) {
       $("#mydropdown").append($("<option></option>",{value:value["rb_communityId"],text:value["rb_communityLabel"]}));
    });
    

    I dont find uses for your other two variables, the above will add value and text.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题