我有一个页面,访问者可以使用下拉菜单来获取有关某人的信息。
在更改调用之后,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/