Tôi có một chức năng ở đây xác nhận các trường trong biểu mẫu nếu chúng trống.Cách kiểm tra giá trị trả về của hàm nếu đúng hoặc sai
function ValidateForm()
{
jQuery('span.error_msg').hide();
var success = true;
jQuery("#shippingF input").each(function()
{
if(jQuery(this).val()=="")
{
jQuery(this).next().show();
success = false;
}
});
return success;
}
Bây giờ tôi muốn sử dụng chức năng ở đây:
function post(url,formId) {
jQuery("#process").html('<img src="<?php echo get_bloginfo('wpurl').'/wp-content/plugins/'.basename(dirname(__FILE__)).'/images/co/ajax-loader.gif'; ?>" alt="loading" title="ajax-loader" width="16" height="16" class="alignnone size-full wp-image-134">');
jQuery.post(url, jQuery('#' + formId).serialize(), function(d) {
jQuery('html,body').animate({scrollTop: jQuery("#scrollhere").offset().top},'slow');
jQuery("#response").html('<center><p style="height:820px"><span style="color:black;font-weight:bold;font: 11px arial,verdana,sans-serif;"><b>Loading available payment getaways..</b></span><br/><img src="<?php echo get_bloginfo('wpurl').'/wp-content/plugins/'.basename(dirname(__FILE__)).'/images/co/8-1.gif'; ?>" width="220" height="19" /></p></center>');
jQuery("#response").load("<?php echo get_bloginfo('wpurl').'/wp-content/plugins/'.basename(dirname(__FILE__)).'/checkout_payment.php'; ?>", function() { Cufon.refresh(); });
jQuery("#response").attr("style","height:1030px");
});
}
Tôi đã thử nó và tôi đưa ra với điều này.
function post(url,formId) {
ValidateForm();
if(ValidateForm() == 'false') {
jQuery('html,body').animate({scrollTop: jQuery("#shippingF").offset().top},'slow');
} else {
jQuery("#process").html('<img src="<?php echo get_bloginfo('wpurl').'/wp-content/plugins/'.basename(dirname(__FILE__)).'/images/co/ajax-loader.gif'; ?>" alt="loading" title="ajax-loader" width="16" height="16" class="alignnone size-full wp-image-134">');
jQuery.post(url, jQuery('#' + formId).serialize(), function(d) {
jQuery('html,body').animate({scrollTop: jQuery("#scrollhere").offset().top},'slow');
jQuery("#response").html('<center><p style="height:820px"><span style="color:black;font-weight:bold;font: 11px arial,verdana,sans-serif;"><b>Loading available payment getaways..</b></span><br/><img src="<?php echo get_bloginfo('wpurl').'/wp-content/plugins/'.basename(dirname(__FILE__)).'/images/co/8-1.gif'; ?>" width="220" height="19" /></p></center>');
jQuery("#response").load("<?php echo get_bloginfo('wpurl').'/wp-content/plugins/'.basename(dirname(__FILE__)).'/checkout_payment.php'; ?>", function() { Cufon.refresh(); });
jQuery("#response").attr("style","height:1030px");
});
}
}
Vấn đề là, xác thực đang hoạt động .. tuy nhiên, chức năng chạy .post()
mặc dù có trường trống. Tôi đoán nó trên nếu/điều kiện khác .. là có một cách tốt hơn để thực hiện điều này?
Cảm ơn bạn.
Có lý do nào bạn đang gọi ValidateForm hai lần không? –
Vấn đề là, nếu tôi không bao gồm 'ValidateForm();' trên 'post()' chức năng .. các thông báo lỗi không hiển thị .. – Ken
Nó đã được gọi bên trong if. Bạn không cần phải gọi lại trước đó. –