2013-10-09 88 views
0

出於某種原因,此表單不斷刷新頁面,我不知道爲什麼,因爲我有另一種形式在頁面上使用不同的id,但相同的腳本,它不刷新。我曾嘗試使用preventDefault();而且它也不起作用。有人能告訴我在這種形式或腳本中出錯的地方嗎?Jquery表單提交保持刷新

感謝

首頁

<script type="text/javascript" src="../JS/jquery.js"></script> 
<script type="text/javascript" src="../JS/jquery-ui.js"></script> 
<script type="text/javascript" src="../JS/member_info.js"></script> 

形式

<form id="central"> 
<table width="40%" class="search"> 
<tr> 
<td width="39%" align="left"><p><b>Inception Date:</b></p><input size="10" maxlength="10" id="creddate" type="text" value="<?php echo $creddate; ?>" /></td> 
<td width="41%" align="left"><p><b>Surety:</b></p><input size="15" maxlength="15" id="surety" type="text" value="<?php echo $surety; ?>" /></td> 
<td width="20%" align="left"><p><b>Credit Limit:</b></p><input size="15" maxlength="15" id="creditlimit" type="text" value="<?php echo $credlimit; ?>" /></td> 
</tr> 
</table> 
<br /> 
<table style="margin:0 auto"> 
<tr> 
<td><input type="hidden" id="code" size="12" readonly="true" value="<?php echo $code; ?>" /></input></td> 
<td><input type="submit" value=" Save " /></input></td> 
<td><p align="center" id="central_status"></p></td> 
</tr> 
</table> 
</form> 

腳本 - 從member_info.js鏈接

$(document).ready(function(){ 
    $("form#central").submit(function() { 
    var code = $('#code').val(); 
    var inception = $('#creddate').val(); 
    var surety = $('#surety').val(); 
    var credit = $('#creditlimit').val(); 
    $.ajax ({ 
     type: "POST", 
     url: '../members/edit_central.php', 
     data: {code: code, creddate: creddate, surety: surety, creditlimit: creditlimit}, 
     success: function(data) { 
     $('#central_status').html(data); 
     } 
    }); 
    return false; 
    }); 
}); 
+0

使用輸入鍵入按鈕已提交 –

回答

3

確保您preventDefault()表格提交後立即進行。在做之前不要做任何其他事情(如調用Ajax)。

例如爲:

$("form#central").submit(function(e) { 
    e.preventDefault(); 

    // Your other long running (possibly async) 
    // operations here 
} 

此外,還要確保你沒有動態地添加表單頁面。如果您這樣做,則需要現場附加提交事件。像這樣:

$(document).on('submit', 'form#central', function(e){ 
    // same as above 
}); 
+0

現在不刷新,謝謝:) – bass71982

0

使用輸入型按鈕,在點擊事件處理結束簡單的返回假insted的的提交

<input type="button" value=" Save " name="saveButton" id="saveButton" /> 
0

做例如招:

$('form input[type="submit"]').click(function() { 
.... 
return false; 
})