繁体   English   中英

$(id).submit()不触发表单的提交

[英]$(id).submit() not triggering submission of form

我有一个表单,当用户单击提交按钮时,需要做两件事。 第一个是给我发送电子邮件,第二个是将用户发送到PayPal。 我已经设置好它,因此它首先向另一个页面发出Ajax调用,然后该页面发送电子邮件。 该页面返回成功后,将调用一个函数来触发表单的提交。

我在表单中放置了一个标准按钮,而不是提交按钮,还有一个onclick甚至可以检测何时单击。 一切正常,直到我尝试在Ajax调用之后提交表单为止,此时表单尚未提交。 这是我的JavaScript:

$('#submit-btn').on('click', function() {

    var err = false;
    $('input, textarea', '#booking-form').each(function(index, element) {
        if($(this).is('[required]') && $(this).val() < 2) {
            alert('Please enter your ' + $(this).prop('placeholder').toLowerCase() + ' before continuing.');
            $(this).focus();
            err = true;
            return false;
        }
    });

    if(err) {
        return false;
    }

    if($('#date').val() == 0) {
        alert('You must choose a date before booking');
        return false;
    }

    ShowLoadingScreen();

    var data = {
        'name': $('#name').val(),
        'address': $('#address').val(),
        'tel': $('#tel').val(),
        'email': $('#email').val(),
        'course': $('#course').find('option:selected').text(),
        'location': $('#location').find('option:selected').text(),
        'date': $('#date').find('option:selected').text()
    };
    AjaxHandler('book-course.php', data, "POST", true);
    //AjaxHandler sends the data off to book-courses.php, which then returns a call to BookingSuccess(). 
    //This is working fine as evidenced by the console.log line showing in the console.

});


function BookingSuccess() {

    $('#booking-form').submit();
    console.log('Booking success called...');

}

AjaxHandler是一个自定义函数,该函数调用页面,然后处理某些响应,包括调用函数。 正在调用BookingSuccess,因为正在控制台中输出名为...的预订成功。 但是,该表单尚未提交。

我在这里创建了jsFiddle的问题


我尝试过的

到目前为止,我已经尝试了以下方法:

  • 在Ajax周围跳过-没有效果
  • 使用按钮直接提交表单,而无需使用Ajax-表单工作正常。
  • 删除了除表单以外的所有HTML-无效
  • 尝试在#submit-btn点击事件的顶部提交表单-无效
  • 将return true函数放在$('#booking-form').submit() -不起作用
  • 使用较长版本的jQuery触发器$('#booking-form').trigger('submit') -无效。
  • console.log事件放入onsubmit=""表单中以查看是否触发了该表单-在小提琴上有效,但在网站上无效。

我对为什么不提交此表格感到困惑。 没有什么可以阻止它。 我们正在通过那条线。 我在FireBug中休息了一下,然后介入,我们对该功能进行了完善。 它只是不做它的工作。

如何获得此表格以正确提交?

经过进一步检查(聊天中有一些死胡同和红色鲱鱼),页面似乎包含两个具有相同ID的元素。 换一个,你就很黄金。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM