我在取消綁定後嘗試將點擊綁定到按鈕時遇到問題。解除綁定後無法綁定點擊
當代碼文本字段中存在更改事件時,該按鈕將被取消綁定。該更改將檢查數據庫中是否存在重複。如果沒有重複,它將綁定點擊保存按鈕,但它不起作用。
jQuery代碼
$(document).ready(function() {
$('#glaccountssomgr_save_btn').on('click', submitGLAccount);
$('#glaccountssomgr_acount_code').change(function(event) {
$('#glaccountssomgr_save_btn').off('click', submitGLAccount);
var inputCode = $('#glaccountssomgr_acount_code').val();
$.ajax({
url: '<?php echo Router::url(array('controller' => 'GeneralLedgerAccountsSOMgr', 'action' => 'glaCheckDuplicate', '')); ?>',
data: {code: inputCode},
type: 'POST',
success:function(data)
{
if(data)
{
var dupResults = jQuery.parseJSON(data);
if(dupResults.isDuplicate)
{
$('#glaccountssomgr_save_btn').off('click');
alert("The GL Account is already in use. Please use another Code");
$('#glaccountssomgr_acount_code').focus();
return false;
}
else
{
alert("No duplicate");
//the below does not get call
$('#glaccountssomgr_save_btn').on('click', submitGLAccount);
}
}
else
{
alert("Error in checking GL Account Code. Please try again");
return false;
}
},
error:function(data)
{
alert("Unexpected Error. Please try again");
}
});
});
});
function submitGLAccount()
{
//performs validation checking and data submission
alert("I'm here");
}
保存按鈕
<a href="#" id="glaccountssomgr_save_btn" name="glaccountssomgr_save_btn" data-
role="button" data-icon="check" data-inline="true" data-ajax="false" data-theme="b"
data-iconpos="right">Save</a>
保存按鈕再次僅重新綁定將IF操作被點擊兩次,如果焦點從代碼去其他表單元素。
我做錯了什麼?
我會避免去綁定和重新綁定的事件處理程序。改爲設置切換變量。 – ahren