【发布时间】:2011-06-23 09:13:13
【问题描述】:
我的团队要求我构建一个系统,作为我们下一个项目的编程原型,该项目将使用 codeigniter 和 jquery 开发。由于我是网络编程的新手,我需要相当长的时间来解决问题。而且下面的代码困扰了我两天。
$(function() {
var bln_valid = true;
$('#btn_save').click(function() {
$('.warning-label').html('');
if ($('#txt_customer_code').val() == '') {
// did user provide customer code?
$('#lbl_customer_code').html('Customer Code has to be filled in.');
bln_valid = false;
} else {
// is the code unique?
$.post('<?php echo base_url()."index.php/customer/is_code_exists" ?>', {
txt_customer_code : $('#txt_customer_code').val()
},
function(data) {
if(data.error == undefined) {
if (data['code_exists'] == 1) {
$('#lbl_customer_code').html('Customer Code is not available.');
bln_valid = false;
}
} else {
$('#lbl_customer_code').html('Customer Code is not available.');
bln_valid = false;
}
},'json'
);
}
if (bln_valid == false) {
return false;
} else {
// insert the code to database
$.post('<?php echo base_url()."index.php/customer/add" ?>', {
txt_customer_code : $('#txt_customer_code').val(),
},
function(data) {
if(data.error == undefined) {
$('#lbl_customer_code').html(data['key']);
if (data['key'] == '') {
$('#lbl_customer_code').html('Failed Saving.');
return false;
} else {
$('#lbl_customer_code').html('Successfully saving.');
}
} else {
$('#lbl_customer_code').html('Failed Saving.');
return false;
}
},'json');
}
});});
所以我想在将客户代码插入数据库之前验证其唯一性。但似乎都使用 ajax 的验证和插入代码几乎同时执行。 bln_valid 在开始插入时仍保留其 TRUE 值,即使客户代码不是唯一的。
- 有没有办法让插入代码在验证完成后运行?我已阅读有关 jquery 队列的信息,但未能掌握该概念并将其应用于解决我的问题。
- 有没有更有效的方法来达到验证和提交数据的目的?
我尽量不使用任何插件,因为我被告知要尽可能少地使用任何外部作品。
感谢您的帮助。
【问题讨论】:
标签: jquery ajax validation submit