我想在阿贾克斯成功后的特定TD显示图像,但它不能显示图像也没有工作的人指导我是我出了问题下面是我的code
AJAX:
<脚本>
$(文件)。就绪(函数(){
$('。EDIT1')。在('改变',函数(){
ARR = $(本).attr(类)分裂()。
。VAR客户端ID =的document.getElementById(客户)的价值;
。ACCOUNT_ID =的document.getElementById(ACCOUNT_ID)的价值;
。$(本).parent()下一个()找到(输入:复选框)ATTR(选中,真正的);
$阿贾克斯({
键入:POST,
网址:clientnetworkpricelist / routestatusupdate.php
数据:值=+ $(本).VAL()+&安培;行ID =+常用3 [2] +与&字段=+常用3 [1] +与&的clientid =+的clientid + &功放; ACCOUNT_ID =+ ACCOUNT_ID,
成功:函数(结果){
数据= jQuery.parseJSON(结果); //添加行
VAR OBJ =数据;
$('#CPH_GridView1_Status'+ ARR [2]),空()。
$('#CPH_GridView1_Status'+改编[2])追加(data.status)。
$('。AJAX)HTML($(本).VAL());
$('。阿贾克斯)removeClass移除(AJAX)。
}
});
});
});
< / SCRIPT>
routestatusupdate.php
{
/ **
一些功能用于显示的结果
** /
$结果=阵列();
$结果['状态'] ='< IMG SRC =图像/'。$状态'f.png/>';
$结果['seleniumrouteupdate'] = $ seleniumrouteupdaterow;
$结果['路由更新'] = $路由更新;
回声json_en code($结果);
}
我routestatusupdate.php出来放样子
{地位:< IMG SRC = \图像\ /Increasef.png \\ />中,seleniumrouteupdate:1,路由更新 :100} {地位:< IMG SRC = \图像\ /Decreasef.png \\ />中,seleniumrouteupdate:1,路由更新:100}
解决方案
您不需要此行数据已经是JSON格式为:
数据= jQuery.parseJSON(结果); //添加行
通过移除这个你还需要更新您的VAR是这样的:
VAR OBJ =结果;
第二件事,你的JSON是格式错误..它应该是这样的输出:
[{地位:< IMG SRC = \图像\ /Increasef.png \\ />中,seleniumrouteupdate:1,路由更新:100},{地位:< IMG SRC = \图像\ /Decreasef.png \\ />中,seleniumrouteupdate:1,路由更新:100} ]
您可能有回音json_en code($结果);在两个地方,你需要将它们合并在一个响应阵列和回声json_en code($结果);
下面是例子你如何能做到这一点(PHP):
函数的东西($地位,$ seleniumrouteupdaterow,$路由更新){
$结果=阵列();
$结果['状态'] ='< IMG SRC =图像/'。$状态'f.png/>';
$结果['seleniumrouteupdate'] = $ seleniumrouteupdaterow;
$结果['路由更新'] = $路由更新;
返回$结果;
}
// .... PHP code
$ JSON =阵列();
$ JSON [] =东西($地位,$ seleniumrouteupdaterow,$路由更新);
// ...一些其他的code
$ JSON [] =东西($地位,$ seleniumrouteupdaterow,$路由更新);
回声json_en code($ JSON);
和结束时,你也需要更新该行:
$('#CPH_GridView1_Status'+ ARR [2])追加(data.status);
由于data.status是你需要瞄准正确元素的数组(第一或第二):
$('#CPH_GridView1_Status'+ ARR [2])追加(data.status [0])。
这将追加的第一状态消息。
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:
<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
{
/**
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
{"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:
data = jQuery.parseJSON(result); //added line
By removing this you also need to update your var like this :
var obj = result;
Second thing, your JSON is in wrong format.. it should be output like this :
[{"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); in two places, you need to combine them in one response array and echo json_encode($results);
Here is example of how you can do this (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):
$('#CPH_GridView1_Status' + arr[2]).append(data.status[0]);
That would append your first status message..
下一篇:不能读取文本框的值文本框