简体   繁体   English

如何在hubspot上停止表单提交?

[英]How to stop form submission on hubspot?

I have form from which if you enter company name and the validation is correct then it will redirect to the company page but if misspelled company name or company name is not available then it shows error. 我有一个表格,如果您输入公司名称并且验证正确,那么它将重定向到公司页面,但如果拼写错误的公司名称或公司名称不可用,则显示错误。 now the validation is working fine but the form submission in not stopping and it show me thank you message. 现在验证工作正常,但表单提交不停止,它显示我谢谢你的消息。

I tried return false & also e.preventDefault(); 我试过return falsee.preventDefault(); but none of them are working. 但他们都没有工作。 The form submits data through ajax. 表单通过ajax提交数据。

here is the JS code 这是JS代码

$(window).load(function() {
    var gett;
    var op_value = [];
    var op_urls = [];
    var op_spell = [];
    $(".hs_company input").keyup(function() {
        gett = $(this).val().replace(/\s/g, '');
        $('.sidebar-form input[type=hidden]').val(gett).change();
    });

    $(".sidebar-form form").on("submit", function(e) {
        e.preventDefault();
        $('.hs_company_list option').each(function() {
            op_spell.push($(this).text().substring(0, 3));
            op_value.push($(this).text().replace(/\s/g, ''));
            op_urls.push($(this).val());
        });
        var sliced_str = gett.substring(0, 3);
        if ($.inArray(gett, op_value) !== -1) {
            var op_data = $.inArray(gett, op_value);

            setInterval(function() {
                window.location.href = op_urls[op_data];
            }, 2000);
            return false;
        } else if ($.inArray(sliced_str, op_spell) > -1) {

            $(".sidebar-form .hs-button").before("<label>Misspelled Company Name<label>");

            return false;
        } else {

            $(".sidebar-form .hs-button").before("<label>Company Not Found<label>");

            return false;
        }
        return false;
    });
});

You have to disable the submit event on the form after it has already been submitted. 您必须在表单提交后禁用表单上的提交事件。 In your code $(".sidebar-form form").submit(function(e) you're automatically submiting the form. You can use on submit or try to put a condition in your function. exs: 在您的代码$(".sidebar-form form").submit(function(e)您自动提交表单。您可以在提交时使用或尝试在您的函数中添加条件.exs:

with condition: 条件:

$(".sidebar-form form").submit(function () {

    var form = $(".sidebar-form form");

    if (form.validate() === true) {
        $(this).submit(function () {
            return false;
        });
        return true;
    }
    else {
        return false;
    }
});

or with on submit : 或者提交:

$(".sidebar-form form").on("submit", "form", function() {
$(this).submit(function() {
    return false;
});
return true;
});

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

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