[英]how to display image in td after ajax success
I want to display the image in the particular td after ajax success but it not displaying the image it not working anyone guide me were i went wrong below is my code 我想在ajax成功后在特定的td中显示图像,但是它不显示图像却无法正常工作,任何人都指导我,如果我在下面出错了,这是我的代码
ajax: 阿贾克斯:
<script>
$(document).ready(function() {
$('.edit1').on('change', function() {
arr = $(this).attr('class').split(" ");
var clientid = document.getElementById("client").value;
account_id = document.getElementById("account_id").value;
$(this).parent().next().find('input:checkbox').attr("checked", true);
$.ajax({
type: "POST",
url: "clientnetworkpricelist/routestatusupdate.php",
data: "value=" + $(this).val() + "&rowid=" + arr[2] + "&field=" + arr[1] + "&clientid=" + clientid + "&account_id=" + account_id,
success: function(result) {
data = jQuery.parseJSON(result); //added line
var obj = data;
$('#CPH_GridView1_Status' + arr[2]).empty();
$('#CPH_GridView1_Status' + arr[2]).append(data.status);
$('.ajax').html($(this).val());
$('.ajax').removeClass('ajax');
}
});
});
});
</script>
routestatusupdate.php routestatusupdate.php
{
/**
some function for display the result
**/
$result=array();
$result['status']='<img src="image/' . $status . 'f.png" />';
$result['seleniumrouteupdate']=$seleniumrouteupdaterow;
$result['routeupdate']=$routeupdate;
echo json_encode($result);
}
my routestatusupdate.php out put looks like 我的routestatusupdate.php看起来像
{"status":"<img src=\"image\/Increasef.png\" \/>","seleniumrouteupdate":"1","routeupdate":"100"}{"status":"<img src=\"image\/Decreasef.png\" \/>","seleniumrouteupdate":"1","routeupdate":"100"}
You don't need this line as data is already in JSON format: 您不需要此行,因为数据已经是JSON格式:
data = jQuery.parseJSON(result); //added line
By removing this you also need to update your var like this : 通过删除它,您还需要像这样更新var:
var obj = result;
Second thing, your JSON is in wrong format.. it should be output like this : 第二件事,您的JSON格式错误。.应该这样输出:
[{"status":"<img src=\"image\/Increasef.png\" \/>","seleniumrouteupdate":"1","routeupdate":"100"},{"status":"<img src=\"image\/Decreasef.png\" \/>","seleniumrouteupdate":"1","routeupdate":"100"}]
You probably have that echo json_encode($result); 你可能有回声json_encode($ result); in two places, you need to combine them in one response array and echo json_encode($results);
在两个地方,您需要将它们组合到一个响应数组中,然后回显json_encode($ results);
Here is example of how you can do this (PHP): 以下是如何执行此操作(PHP)的示例:
function something($status, $seleniumrouteupdaterow,$routeupdate) {
$result=array();
$result['status']='<img src="image/' . $status . 'f.png" />';
$result['seleniumrouteupdate']=$seleniumrouteupdaterow;
$result['routeupdate']=$routeupdate;
return $result;
}
// ....PHP code
$json = array();
$json[] = something($status, $seleniumrouteupdaterow,$routeupdate);
// ... some other code
$json[] = something($status, $seleniumrouteupdaterow,$routeupdate);
echo json_encode($json);
And at end you need also to update this line: 最后,您还需要更新此行:
$('#CPH_GridView1_Status' + arr[2]).append(data.status);
Since data.status is an array you need to target the right element (first or second): 由于data.status是一个数组,因此您需要定位正确的元素(第一个或第二个):
$('#CPH_GridView1_Status' + arr[2]).append(data.status[0]);
That would append your first status message.. 这将附加您的第一条状态消息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.