在此的jsfiddle這個JavaScript爲什麼會失敗?
有我的條紋下來的問題。
嘗試點擊「保存」,你會看到你得到一個提醒。
現在點擊「簽名」或「A」。這些行現在使用TableSorter排序。
現在點擊「保存」,然後什麼也沒有發生,我預計會再次看到警告框。
問題
有人能想出解決的JavaScript,因此該行已被排序即使在保存按鈕的工作原理?
HTML
<div class="page-body">
<table class="alerts tablesorter" id="accTable" cellspacing="0">
<thead>
<tr class="header">
<th class="activity-header"> A </th>
<th class="activity-header"> Signed </th>
<th class="activity-header"> </th>
</tr>
</thead>
<tbody>
<form action="" method="post">
<input name="anchor" value="7249" type="hidden">
<tr class="row" id="7249">
<td class="activity-data">7249</td>
<!-- tablesorter can't sort a column with check boxes out-of-the-box, so it needs something to sort on. That is why the span tag is here -->
<!-- a jquery script is watching the state of the checkbox, so when clicked the value in the span is updated -->
<td class="checkbox"> <span style="display:none;">0</span> <input name="signed" type="checkbox" > </td>
<td class="edit-column"> <input value="Save" type="submit"></td>
</tr>
</form>
<form action="" method="post">
<input name="anchor" value="61484" type="hidden">
<tr class="row" id="61484">
<td class="activity-data">61484</td>
<td class="checkbox"> <span style="display:none;">1</span> <input name="signed" type="checkbox" checked > </td>
<td class="edit-column"> <input value="Save" type="submit"></td>
</tr>
</form>
</tbody>
</table>
</div>
的JavaScript
$(document).ready(function() {
$("#accTable").tablesorter();
// :checkbox stops from executing the event on the save button. Same as input[type=checkbox]
$('#accTable input:checkbox').click(function() {
// insert 1 or 0 depending of checkbox state in the tag before the input tag. In this case <span> is before <input>
// this is done so tablesorter have something to sort on, as it doesn't support checkbox sort out of the box.
var order = this.checked ? '1' : '0';
$(this).prev().html(order);
$(this).parents("table").trigger("update");
});
});
// sends the form content to server side, and stay on page
$('form').live('submit', function() {
alert('test');
// don't redirect
return false;
});
正如答案所說,你不能在'tbody'裏面有'form'。 Firefox甚至可以從一開始就糾正錯誤,因此在Firefox中,單擊按鈕將重新加載頁面並且不顯示警報。 –