【发布时间】:2013-04-11 16:28:06
【问题描述】:
我在 jQuery 中执行 $.post() 并收到类似于以下内容的 <tr> 元素响应:
<tr class="editable" id="527a84eb-b430-4a33-9079-a19d00ca373c">
<td class="date">2013-04-05
</td>
<td class="amountType">Credit
</td>
<td class="amount">$5.00
</td>
<td class="category">Meals
</td>
<td class="notes"></td>
<td data-expense-report-line-item-id="527a84eb-b430-4a33-9079-a19d00ca373c" class="actionItems">
<button class="deleteLineItem">Delete</button>
<button class="editLineItem">Edit</button>
</td>
</tr>
我打算用这个函数在我的表中注入这一行:
$.post('@Url.Content("~/ExpenseReports/SaveLineItem")', {
Date: date,
Amount: amount,
AmountType: amountType,
ExpenseCategoryId: category,
Notes: notes,
ExpenseReportId: expenseReportId,
ExpenseReportLineItemId: expenseReportLineItemId
}, function (data) {
if ($('table tbody').length > 1) {
$('table tbody tr:last').slideUp(200, function () {
$(data).insertAfter('table tbody tr:first').slideDown();
});
} else {
alert(data);
$(data).appendTo($('table tbody'));
}
row.find('td.date:first input.date').val('');
row.find('td.amount:first input.amount').val('');
row.find('td.amountType:first select.amountType').val('Debit');
row.find('td.category:first select.category').val('');
row.find('td.notes:first input.notes').val('');
}, 'html');
这里的目的是在tbody 元素的第一行之后插入这一行。但是,当我尝试这个时,我得到了一个 javascript 错误:
Uncaught Error: Syntax error, unrecognized expression: <tr class="editable" id="527a84eb-b430-4a33-9079-a19d00ca373c">
<td class="date">
2013-04-05
</td>
<td class="amountType">
Credit
</td>
<td class="amount">
$5.00
</td>
<td class="category">
Meals
</td>
<td class="notes">
</td>
<td data-expense-report-line-item-id="527a84eb-b430-4a33-9079-a19d00ca373c" class="actionItems">
<button class="deleteLineItem">Delete</button>
<button class="editLineItem">Edit</button>
</td>
</tr>
在我看来,这应该可以被 jQuery 解析。我做错了什么?
【问题讨论】:
-
我不认为
<tr>s 可以滑下来,但这不会是你发现的问题 -
在这里使用 firebug(所以无论使用什么版本的 jQuery SO)它都能很好地解析你的 html(运行
$('<tr> ..your stuff .. </tr>')) -
嗯……不知道是不是 Chrome 的问题……
-
这是一个 jsfiddle,显示您的代码正常工作(大部分未修改)jsfiddle.net/mUExf
-
不,对我来说,它在 FireFox 中也失败了,消息相同。我正在使用 jquery 1.9.0,也许这就是问题所在。
标签: jquery asp.net-mvc