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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化