在我的ajax中,PHP端包含一个包含3个值的数组。我想将这3个值放入单独的输入字段中。我怎样才能做到这一点?如何访问从PHP发送的数组?
到目前为止我的代码:
PHP
$total['tot1'] = $numwelds + $numconwelds + $mpcountrys ;
$total['tot2'] = $numwelds + $numconwelds + $fortot2 ;
$total['tot3'] = $numwelds + $numconwelds + $fortot2 / $fortot3;
$response = json_encode($total);
header("Content-Type: application/json");
echo $response;
exit;
Jquery的
jQuery(document).ready( function($) {
(function($) {
$(document).ready(function(){
$('#formsubmit').click(function(){
$.post(
PT_Ajax.ajaxurl,
{
action : 'ajax-inputtitleSubmit',
numberofwelds : $('input[name=numberofwelds]').val(),
numberofconwelds : $('input[name=numberofconwelds]').val(),
nextNonce : PT_Ajax.nextNonce
},
function( response ) {
$("#totalone").val(response);
$("#totaltwo").val(response);
$("#totalthree").val(response);
}
);
return false;
});
});
})(jQuery);
});
答案 0 :(得分:1)
您可以使用JSON.parse
将json转换为object:
var obj = JSON.parse(response);
$("#totalone").val(obj.tot1);
$("#totaltwo").val(obj.tot2);
$("#totalthree").val(obj.tot3);
或者您也可以使用jQuery的$.parseJSON()
。
答案 1 :(得分:0)
使用
$("#totalone").val(response["tot1"]);
$("#totaltwo").val(response["tot2"]);
$("#totalthree").val(response["tot3"]);