javascript - 如何在jquery中对php/ajax传递的数组进行切片

标签 javascript php jquery html ajax

我有一个页面,访问者可以使用下拉菜单来获取有关某人的信息。

在更改调用之后,jquery 通过 ajax 将人员的 ID 发送到 php 脚本,该脚本从数据库中提取数据。

因为数据应该在请求页面上的三个不同的 div 中使用,所以数据作为 (php) 数组发送回。

现在我需要你的帮助。如何将数组(它有三个部分)拆分为三个 javascript 部分,以便将其发送到特定的 div(so、div1、div2 和 div3)?

这是我的代码:

 $('#contentz').change(function(){
    var Value = $(this).val();  
    $.ajax({
        type: 'post',
        url: '/ajax.php',
        dataType: 'html',
        data: {action: Value},
        success:function( data){ 
    // I need to slice the array here           
            $("#veld1").html( data ); // result 1
            $("#veld2").html( data ); // result 2
            $("#veld3").html( data ); // result 3
    //alert(data); 
        }, 
        error:function (xhr, ajaxOptions, thrownError){
            //On error, we alert user
            alert(thrownError);
        }, 
        complete: function(){
            //alert('update success'); 
        }
    });
});


#contentz is the select/ dropdown.
#veld1, #veld2 and #veld3 are the divs that display the result.

最佳答案

将数组返回到 AJAX 的最简单方法之一是将其编码为 JSON ,

$array = array("firstDivValue","secondValue","thirdDivValue");
echo json_encode($array);

并通过 AJAX 成功访问它,

$.ajax({
        .......
        //Set it as JSON as we are now returning it
        dataType: 'json',
        .........
        success : function(data){

                $.each(data,function(i,value){
                    //Get value here
                    alert(value);
                });
         }

关于javascript - 如何在jquery中对php/ajax传递的数组进行切片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28340255/

相关文章:

javascript - Leaflet 相当于 google toSpan() 函数

javascript - Jquery 循环和递增

php - 费率结构的数据库/算法

php - Yii,不使用隐藏字段设置模型值

php - 添加 PHP Prestashop 模块后获取产品 ID

Jquery Collage 在悬停时调整图像大小

javascript - 刷新页面时 hasClass 不起作用

javascript - 使用 Parsley.js 验证选择的多项选择

javascript - 鼠标悬停在两个元素之间的问题

javascript - 仅自动提交表单一次