Ken*_*Ken 15 forms jquery if-statement
我在这里有一个函数,如果它们是空的,则验证表单中的字段.
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;
}
Run Code Online (Sandbox Code Playgroud)
现在我想在这里使用该功能:
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");
});
}
Run Code Online (Sandbox Code Playgroud)
我试过了,我想出了这个.
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");
});
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,验证工作正常..但是,.post()即使存在空字段,函数也会运行.我猜它在if/else条件..是否有更好的方法来实现这一目标?
谢谢.
Chr*_*ian 20
false != 'false'
Run Code Online (Sandbox Code Playgroud)
为了获得良好的度量,请将validate的结果放入变量中以避免双重验证并在IF语句中使用它.像这样:
var result = ValidateForm();
if(result == false) {
...
}
Run Code Online (Sandbox Code Playgroud)
JDa*_*ips 11
你不需要打电话ValidateForm()两次,如上所述.你可以这样做
if(!ValidateForm()){
..
} else ...
Run Code Online (Sandbox Code Playgroud)
我认为这将解决上面的问题它看起来像你true/false的字符串等效'false'.
您将结果与字符串(“假”)而不是内置负常量(假)进行比较
只需使用
if(ValidateForm() == false) {
Run Code Online (Sandbox Code Playgroud)
或者更好
if(!ValidateForm()) {
Run Code Online (Sandbox Code Playgroud)
另外为什么你要调用 validateForm 两次?
| 归档时间: |
|
| 查看次数: |
93628 次 |
| 最近记录: |