jQuery and returned HTML
This is probably a no-brainer but I'm stuck.
I've a function which creates a table:
function getTable(someJSON, someGroup){
var table = $("<table>").attr("class", "someTable");
var row1 = $("<tr>");
var cell1 = $("<td>").text(someJSON.someThing);
row1.append(cell1);
var cell开发者_开发百科2 = $("<td>").text(someJSON.someThingElse);
row1.append(cell2);
var row2 = $("<tr>");
row2.append("<td>Something</td><td>Something else</td>");
table.append(row2);
var row3 = $("<tr>").append(getRow(someJSON, someGroup));
table.append(row3);
return table;
}
The table is returned okay but there's nothing in the last row.
getRow is like this:
function getRow(someJSON, someGroup){
$.ajax({
url: 'getRow.php',
data: ({
DM: JSON.stringify(someJSON),
someGroup: someGroup
}),
async: false
})
}
I've checked with FireBug and the getRow.php returns a valid result like: 123234 after doing it's thing but I'll be blown if I can get it into the ... I've been thrashing around with it and in an earlier iteration I had something like [object Object] appended to the row.
Any help would be greatly appreciated.
Cheers,
Dom
getRow
does not return anything - that's why your last row is empty :)
But even after you'll fix it - you append it to the TR without a TD inside, so it will not appear in the right place on the screen
Don-Joy was right but I miss-read his answer, I've altered getRow to:
function getRow(someJSON, someGroup){
var returnedText;
$.ajax({
url: 'getRow.php',
data: ({
DM: JSON.stringify(someJSON),
someGroup: someGroup
}),
dataType: "html",
async: false,
success: function (data) {
returnedText = data;
}
});
return returnedText;
}
works a treat now. Thanks for the pointer Don-Joy!
Cheers,
Dom
As Don-Joy say, your JavaScript function getRow
isn't returning anything. It doesn't have a return
statement. You'll need to change it to:
function getRow(someJSON, someGroup){
var a = $.ajax({
url: 'getRow.php',
data: ({
DM: JSON.stringify(someJSON),
someGroup: someGroup
}),
async: false
});
return a.responseText;
}
However you should never use synchronized AJAX calls like this. It slows down your page considerably and can even make the browser unresponsive.
精彩评论