現在對jQuery有點問題。我有一個表,我的目標是有一個刪除按鈕,點擊後,它從一個mySQL後端刪除。我在功能方面沒有問題,我的問題是我無法讓jQuery返回false,它會一直重定向到我在form標籤中指向的php腳本。如果您能看到代碼問題,請告訴我。jQuery表單提交不會返回false
我有一個表結構設置,這只是爲了顯示從mysql查詢返回的所有行。也許我沒有正確設置表格。
<?php
if (sizeof($rows5) > 0) {
foreach($rows5 as $row5):
echo"
<tr class='tableRowClass2'>
<td>{$row5['PackingSlipStarmontID']}</td>
<td>{$row5['fullStarmont']}</td>
<form action='delete/starmont.php' method='post' name='deletestarmontForm' id='deletestarmontForm'>
<td>
<button id='button3'>Remove</button>
<input type='hidden' id='PackingSlipStarmontID' name='PackingSlipStarmontID' value='{$row5['PackingSlipStarmontID']}'/>
</td>
</form>
</tr>";
endforeach;
}
?>
這裏是jQuery,它假設提交表單後阻止頁面重定向。
$(document).ready(function() {
$("#button3").click(function() {
$.post($("#deletestarmontForm").attr("action"),
$("#deletestarmontForm :input").serializeArray(),
function(info){
$("#result4").html(info);
});
});
$("#deletestarmontForm").submit(function() {
return false;
});
function clearInput() {
$("#deletestarmontForm :input").each(function() {
$(this).val('');
});
}
});
所以這個代碼在它調用PHP腳本和SQL查詢是精細感。問題是它重定向到那個php頁面,而不是在提交時返回false。
*編輯*** 謝謝大家對你的幫助。在這個和實驗之間,我已經解決了這個問題。似乎問題在於處理重複的ID。我使用的代碼與我發佈的代碼相同,但不是使用ID選擇器,而是通過將其更改爲類選擇器來解決此問題。
'e.preventDefault()'在返回false之前? – Soundz
'Id'應該始終是唯一的。 – Dementic
'事件。preventDefault();'會在點擊函數中執行這個工作 –