我在这里看到了许多例子,并尝试了几个小时让他们工作,但似乎不能弄清楚我做错了什么。我对jquery和ajax非常陌生,所以请友好。
我正在尝试使用smartWizard jquery插件。下面是一些代码
function validateStep1()
{
var isValid = true;
// validate first_name
var first_name = $('#first_name').val();
if(!first_name && first_name.length <= 0)
{
isValid = false;
$('#msg_first_name').html('<?php echo $LANG['field_this_required'] ?>').show();
}
else
{
$('#msg_first_name').html('').hide();
}
// validate last_name
var last_name = $('#last_name').val();
if(!last_name && last_name.length <= 0)
{
isValid = false;
$('#msg_last_name').html('<?php echo $LANG['field_this_required'] ?>').show();
}
else
{
$('#msg_last_name').html('').hide();
}
// validate address
var address = $('#address').val();
if(!address && address.length <= 0)
{
isValid = false;
$('#msg_address').html('<?php echo $LANG['field_this_required'] ?>').show();
}
else
{
$('#msg_address').html('').hide();
}
// validate city
var city = $('#city').val();
if(!city && city.length <= 0)
{
isValid = false;
$('#msg_city').html('<?php echo $LANG['field_this_required'] ?>').show();
}
else
{
$('#msg_city').html('').hide();
}
// validate county
/*
var county = $('#county').val();
if(!county && county.length <= 0)
{
isValid = false;
$('#msg_county').html('<?php echo $LANG['field_this_required'] ?>').show();
}
else
{
$('#msg_county').html('').hide();
}
*/
// validate state
var state = $('#state').val();
if(!state && state.length <= 0)
{
isValid = false;
$('#msg_state').html('<?php echo $LANG['field_this_required'] ?>').show();
}
else
{
$('#msg_state').html('').hide();
}
// validate postal
var postal = $('#postal').val();
if(!postal && postal.length <= 0)
{
isValid = false;
$('#msg_postal').html('<?php echo $LANG['field_this_required'] ?>').show();
}
else
{
$('#msg_postal').html('').hide();
}
// validate home_phone
var home_phone = $('#home_phone').val();
if(!home_phone && home_phone.length <= 0)
{
isValid = false;
$('#msg_home_phone').html('<?php echo $LANG['field_this_required'] ?>').show();
}
else
{
$('#msg_home_phone').html('').hide();
}
if(isValid==true)
{
document.getElementById('step_num').value=1;
loading('<?php echo $LANG['saving']; ?> ',1);
$.post('editprofile.php',$('#editProfile').serialize(),function(data)
{
unloading();
if(!data)
{
isValid = false;
$('#msg_step-1').html("<p style='font-weight:bold;color:#ff0000'><?php echo $LANG['database_update_error']; ?></p>").show();
}
else if(data.status !=1 )
{
isValid = false;
alert(data.message);
$('#msg_step-1').html("<p style='font-weight:bold;color:#ff0000'>"+data.message+"</p>").show();
}
else
{
$('#msg_step-1').html("<p style='font-weight:bold;color:#ff0000'>"+data.message+"</p>").show();
isValid = true;
}
},'json')
.done(function() { alert("done "+isValid); return isValid;})
.fail(function() { alert("fail "+isValid); return isValid;})
;
}
else
{
return isValid;
}
//Commented out the return so that it will only return when done above.
//return isValid;
}
还有其他代码检查此函数的返回值并相应地执行操作。如果任何初始验证检查失败,则其他代码不会执行,因为变量isValid
被设置为false,并且返回false。我的问题是editprofile.php的帖子-代码继续移动,好像函数已经返回了一个真值,即使它还没有返回任何东西。它仍然继续执行$.post
段中的代码,但只有在该函数之外的其他代码运行之后。
我理解代码是以异步方式运行的;然而,我感到困惑的是,为什么毫无例外地,当$('#address').val()
等任何检查返回假值时,它总是得到false
值;但是,它不会从$.post
代码中获得它。为什么等待所有这些返回一个值,而不是等待$.post
代码?
我还对调用代码在返回值之前如何操作感到困惑。
为了便于文档编写,下面是调用该函数的代码:
function leaveAStepCallback(obj){
var step_num= obj.attr('rel');
return validateSteps(step_num);
}
function validateSteps(step)
{
var isStepValid = true;
// validate step 1
if(step == 1){
if(validateStep1() == false ){
isStepValid = false;
$('#wizardvalidate').smartWizard('showMessage','Please correct the errors in step'+step+ ' and click next.');
$('#wizardvalidate').smartWizard('setError',{stepnum:step,iserror:true});
}else{
$('#wizardvalidate').smartWizard('setError',{stepnum:step,iserror:false});
}
}
// validate step 2
if(step == 2){
if(validateStep2() == false ){
isStepValid = false;
$('#wizardvalidate').smartWizard('showMessage','Please correct the errors in step'+step+ ' and click next.');
$('#wizardvalidate').smartWizard('setError',{stepnum:step,iserror:true});
}else{
$('#wizardvalidate').smartWizard('setError',{stepnum:step,iserror:false});
}
}
// validate step3
if(step == 3){
if(validateStep3() == false ){
isStepValid = false;
$('#wizardvalidate').smartWizard('showMessage','Please correct the errors in step'+step+ ' and click next.');
$('#wizardvalidate').smartWizard('setError',{stepnum:step,iserror:true});
}else{
$('#wizardvalidate').smartWizard('setError',{stepnum:step,iserror:false});
}
}
return isStepValid;
}
再一次,我对这一切都是新的,所以请友好!:) 正如你在问题中所说的,$.post
是异步的。
它的回调只会在服务器响应时运行,这保证是在您的其余代码完成执行之后的一段时间。
我也遇到了同样的问题,在几个小时的拉扯后解决了…
var stpflag = false;
function validateSteps(step){
var isStepValid = true;
// validate step 1
if(step == 1){
var returnstatus = savefunctionality();
if((returnstatus == false ) && (stpflag== false)){
isStepValid = false;
$('#wizard').smartWizard('showMessage','Please correct the errors in step'+step+ ' and click next.');
$('#wizard').smartWizard('setError',{stepnum:step,iserror:true});
// $('#wizard').smartWizard.prototype.goBackward();
}else if((returnstatus == false ) && (stpflag== true)){
isStepValid = true;
$('#wizard').smartWizard('setError',{stepnum:step,iserror:false});
$('#wizard').smartWizard('hideMessage','');
showstep2(); // here write your logic for render next page on step-2 div.
stpflag=false;
}
}
return isStepValid;
}
savefunctionality()函数{
var returnVal = false;
$ . ajax (
{
type: 'POST',
dataType: 'json',
url: '/mainfolder/folder/savepage/',
async: true,
data: values,
success: function(json) {
$("#error_msg").html("");
if(json.result.errors != undefined){
if(json.result.errors.length > 0){
itemData = json.result.errors;
returnVal = false;
stpflag = false;
}
} else if(json.result == 1){
returnVal = true;
stpflag = true;
$('a.buttonNext').trigger('click');
}
}
});
return returnVal;
}
这里我使用了两个变量1. returnVal:总是返回false2. Stpflag:创建一个新的全局变量。
明确触发下一个按钮。
希望对你也有用