开发者

Setting TD content using javascript/jquery

I'm trying to replace the contents of a td tag in a javascript function but can't figure out what's going on. Here is the function.

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());
    }
}

I first thought there might be a problem with jquery finding the td controls, so I added the null check.

Then I thought it was the .text() so I tried .html().

I thought maybe it was swallowing an exception so I added the try..catch.

Then I thought it might be an issue with the response object the function received, so I threw some alerts in there and they have the values I need.

I checked t开发者_如何学运维he Ids and made sure they matched the id's found in the html of the page.

I've checked everything I could think of. But my code simply doesn't work. The first one of the two TD elements I'm trying to set contains two links (a tags), but the second one is empty. So I don't think that has anything to do with it. I'm at wits end here trying to figure this out.

Is there something I'm doing wrong? Could there be something else that would cause this behavior?

Thanks in advance.


Since jQuery calls always return a jQuery object they will never be null. To test if a wrapped set is empty or not you can check the length.

ie. if (tdstatus.length > 0 && tdreason.length > 0)

Also, I'm not sure what is invoking this function, but if it's being used as an AJAX callback, the signature for it is actually f(data, textStatus, jqXHR).


I got something sort of similar to your situation working in a jsfiddle:

http://jsfiddle.net/jA4ZQ/1/

Perhaps you can modify that using your code and get it to fail. We can then start from there. Try to keep it as simple and to the point of the problem as possible, it'll make it easier.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜