使用javascript/jquery设置TD内容
我试图在javascript函数中替换td标记的内容,但不知道发生了什么。下面是函数使用javascript/jquery设置TD内容,javascript,jquery,Javascript,Jquery,我试图在javascript函数中替换td标记的内容,但不知道发生了什么。下面是函数 function actionCompleted(response, status, data) { // Set the id for row var rowId = "#rowId-" + response.fieldname; // Set the ids for the two table cells we need var tdstatusId = "#tdstat
function actionCompleted(response, status, data) {
// Set the id for row
var rowId = "#rowId-" + response.fieldname;
// Set the ids for the two table cells we need
var tdstatusId = "#tdstatusId-" + response.id;
var tdreasonId = "#tdreasonId-" + response.id;
alert(tdreasonId);
try {
//get the table cell for the status
var tdstatus = $('#itemDetails').find(rowId).find(tdstatusId);
//get the table cell for the reason
var tdreason = $('#itemDetails').find(rowId).find(tdreasonId);
//Make sure we found our cells
if (tdstatus != null && tdreason != null) {
//Set our cell content
//tdstatus.html(response.ChangeStatus);
//tdreason.text(response.message);
tdstatus.html('TEST');
tdreason.text('TEST');
}
}
catch (e) {
alert(e.toString());
}
}
我首先认为jquery查找td控件可能有问题,所以我添加了空检查
然后我以为是.text()所以我尝试了.html()
我想这可能是一个异常,所以我添加了try..catch
然后我认为函数接收到的响应对象可能有问题,所以我在那里抛出了一些警报,它们有我需要的值
我检查了id,确保它们与页面html中找到的id匹配
我已经检查了我能想到的一切。但我的代码根本不起作用。我试图设置的两个TD元素中的第一个包含两个链接(a标记),但第二个是空的。所以我认为这与此无关。我在这里绞尽脑汁想弄明白这件事
我做错什么了吗?是否还有其他原因会导致这种行为
提前谢谢。我得到了一些类似于您在JSFIDLE工作的情况:
也许你可以用你的代码修改它,让它失败。然后我们可以从那里开始。尽量使它简单明了,并且尽可能直截了当,这会使它变得更容易。因为jQuery调用总是返回jQuery对象,所以它们永远不会为空。要测试包装集是否为空,可以检查长度 即
if(tdstatus.length>0&&tdreason.length>0)
另外,我不确定调用这个函数的是什么,但是如果它被用作AJAX回调,那么它的签名实际上是
f(data,textStatus,jqXHR)
您可以添加HTML的(相关部分)吗?所以var tdstatus和tdreason可以工作吗?这很有帮助。我的表id不正确。我假设,因为我的单元格对象不是空的,并且表行上的查找没有引发异常,所以该行是正常的。结果不是很好听。顺便说一句,如果您选择这样的行:$(“#tdreasonId-”+response.id,“#itemDetails”)
而不是选择3个元素,效率会更高。我不知道这种语法。但这不是跳过了这一行吗?$(“#tdreasonId-”+response.id,“#itemDetails”)中的第二个参数是表的id。是否不需要行id?