0

if 条件中的第一个 e.preventDefault() 不起作用。只有第二个 e.preventDefault() 有效。任何帮助将不胜感激。

我对 jquery 进行了以下更改,但随后表单提交无法正常工作,并且页面停在空白表单操作页面 checkcoupon.php 处,而不是进一步处理表单。

<form name='coupon_discount' id='coupon_discount' action="checkcoupon.php" method='post'>
        <input type="hidden" name="user" value="<?php echo $_SESSION[user_id]; ?>" >
        <input value="" id="couponCode" name="couponcode" class="coupon-placeholder placeholder"  type="text"/>               
        <input type='submit' name='coupon_code_submit' value='Apply' id="cart-coupon" class="sp_apply_btn" >      
  </form>



     jQuery(document).ready(function(){
        jQuery('form#coupon_discount').submit(function(e){
             var   user     = jQuery('input[name="user"]', 'form#coupon_discount').val();
             var couponcode = jQuery('input[name="couponcode"]', 'form#coupon_discount').val();
             if(user != '' && couponcode != '') {
             e.preventDefault();
             var URL = '<?php echo $_conf_vars['ROOT_URL']."ajax.php?usercp=";?>'+user+'&cponcode='+couponcode;
             jQuery.ajax({
                        url:URL,
                        type:'POST',
                        async:true
                      }).done(function(msg,e){
                        if(msg=='Already used!'){ 
                          alert('Coupon code already used!');
                         }else {  
                          jQuery('form#coupon_discount').submit();  
                         }
                      }); 
                   }
                   else{
                     e.preventDefault();
                     alert('Enter a coupon code!');
                   }    
             });  
        });

这是操作页面“checkcoupon.php”的代码。

  session_start();
    ob_start();

    include("includes/files.php");
    include("classes/class.common.php");

    if(isset($_REQUEST['coupon_code_submit']))
    {
        $obj_common = new common;
        $discount = $obj_common->getCouponCodeDiscount($_REQUEST['couponcode'],$_REQUEST['user']); 
        header("location:payment.php");
    }
4

2 回答 2

4

像这样试试

jQuery(document).ready(function(){
    jQuery('form#coupon_discount').submit(function(e){
        e.preventDefault();   //Put here and do the stuff

         });  
    });
于 2013-05-23T12:58:34.313 回答
2

您将e.preventDefault()调用放在异步回调中。当代码到达那里时,已经太晚了。

移动e.preventDefault()外部回调。

于 2013-05-23T12:58:45.093 回答