我正在尝试为我的Magento商店之一运行以下Jquery ajaxSubmit。我使用ajaxSubmit,因为我也有2个文件要从表单上载(意味着我需要$_FILE中的数据)。但是,当我执行ajaxSubmit时,它也会按常规形式提交,这将刷新页面并将我带到下一页,而不是停留在同一页面上的弹出框中,其中包含更多内容。我有return false;可以防止这种情况,但是失败了。
提交代码:

$('#quickbuyform').submit(function (){

    // No Accessories: normal submit
    if ($('#accessories ul li').size()==0) {
        return true;
    }

    strName = "<?php echo $_product->getName() ?>";

    // loading message
    $('#pop_accessories_area_message').html("We are adding '"+strName+"' into your shopping cart ...");
    // Pop with loading
    popAccessories();

    // loading icon
    //var dataString = $('#quickbuyform').serialize();
    $.ajaxSubmit({
          type: "POST",
          url: $('#quickbuyform').attr('action'),
          data: dataString,
          beforeSubmit:  showRequest,
          success: function() {
            // display cart link:
            $('#pop_accessories_area_message').html(strName+" was added to <a href='<?php echo $this->getUrl('checkout/cart') ?>'>your shopping cart</a>.");
            $('.pop_accessories_area_button').show();
            //window.location = '<?php //echo $this->getUrl('checkout/cart') ?>';
          }
        });
    return false;
});


所以这就是我现在使用e.preventDefault()的内容,但是现在它什么都不提交。只需在('#quickbuyform').ajaxSubmit处停止

$('#quickbuyform').submit(function (e){
    e.preventDefault();
    // No Accessories: normal submit
    if ($('#accessories ul li').size()==0) {
        return true;
    }

    strName = "<?php echo $_product->getName() ?>";

    // loading message
    $('#pop_accessories_area_message').html("We are adding '"+strName+"' into your shopping cart ...");
    // Pop with loading
    popAccessories();

    $('#quickbuyform').ajaxSubmit({
          type: "POST",
          url: $('#quickbuyform').attr('action'),
          data: dataString,
          success: function() {
            // display cart link:
            $('#pop_accessories_area_message').html(strName+" was added to <a href='<?php echo $this->getUrl('checkout/cart') ?>'>your shopping cart</a>.");
            $('.pop_accessories_area_button').show();
            //window.location = '<?php echo $this->getUrl('checkout/cart') ?>';
          }
        });
    return false;
});

最佳答案

您可以通过调用事件对象的preventDefault方法来停止常规表单提交。

$('#quickbuyform').submit(function (e) {
    e.preventDefault();
    ...
});

关于ajax - jQuery ajaxSubmit也进行常规表单提交,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13852442/

10-09 19:00