简体   繁体   English

.unbind('submit')。submit()在Chrome / IE中有效,但在Firefox中无效

[英].unbind('submit').submit() works in Chrome/IE but doesn't work in Firefox

I have the following javascript code using jquery 1.9.1: 我有以下使用jQuery 1.9.1的javascript代码:

(function ($) {
$(function () {
    eventTracking();
});

function eventTracking() {
    $("[data-formtracking]").bind('submit', function (e) {
        e.preventDefault();
        var eventName = $(this).data("formtracking");
        rt.trackEvent(eventName);
        $(this).unbind('submit').submit();
    });
}})(jQuery);

Now, when I submit the form, it should make an update to a database, and then follow the action that is on the form, and basically redirect us to the other page. 现在,当我提交表单时,它应该对数据库进行更新,然后按照表单上的操作进行操作,并基本上将我们重定向到另一页。

In Chrome/IE the rt.trackEvent successfully fires and I get redirected to the other site. 在Chrome / IE中,rt.trackEvent成功启动,我被重定向到另一个站点。 In Firefox, I get redirected to the other site, but no database entry. 在Firefox中,我被重定向到另一个站点,但是没有数据库条目。

If I add console.log between each statement, I can see that it is hits all of the log statements. 如果在每个语句之间添加console.log,我可以看到它命中了所有log语句。

So at this point....I'm baffled. 所以在这一点上。。。 I've tried switching from bind to on/off. 我试过从绑定切换为开/关。 I've tried: $('input[type=submit]', this).click() based on a passing comment on another stackoverflow page. 我试过了:$('input [type = submit]',this).click()基于另一个stackoverflow页面上的传递注释。

A few notes: * rt is in an external file. 一些注意事项:* rt在外部文件中。 I've checked and it does exist * If I remove the unbind.submit, then the rt.trackEvent works correctly, except the page doesn't redirect (obviously). 我已经检查并且它确实存在*如果删除unbind.submit,则rt.trackEvent可以正常工作,但页面不会重定向(显然)。 * I've change the unbind.submit() to other variations, but then I have to click the button twice before it submits. *我将unbind.submit()更改为其他变体,但是然后我必须单击两次按钮才能提交。

Thoughts? 有什么想法吗?

Updated 更新

The reason for the e.preventDefault() and later submission is that the form submission was happening too quickly. e.preventDefault()和更高版本提交的原因是表单提交发生得太快。 The page would redirect before rt.trackEvent would work properly. 该页面将在rt.trackEvent正常运行之前重定向。 So I needed to slow the method down by adding a setTimeout($(this).unbind('submit').submit(), 2000); 所以我需要通过添加setTimeout($(this).unbind('submit')。submit(),2000)来减慢该方法的速度。

I removed this to simplify the code, but in hindset it is pretty essential for the working of the code. 我删除了此代码以简化代码,但事后它对于代码的工作非常重要。

Updated 2 Here is the json that is being called: 更新2这是被称为json:

        options = $.extend({ success: $.noop, error: $.noop, urlBuilder: getNoCookieRequestURL, eventName: null }, options);

        options.url = options.urlBuilder(options.url, options.siteId, options.eventName);
        $.getJSON(options.url, function (jsonp) {
            options.success(jsonp);
        }).fail(function () {
            options.error();
        });

Updated 3 更新3

Change getJson to: 将getJson更改为:

            options = $.extend({ success: $.noop, error: $.noop, urlBuilder: getNoCookieRequestURL, eventName: null }, options);

        options.url = options.urlBuilder(options.url, options.siteId, options.eventName);

        $.ajax({
            url: options.url,
            dataType: 'json',
            async: false,
            success: function (jsonp) {
                options.success(jsonp);
            },
            error: function() {
                options.error();
            }
        });

I still seem to be having the speed issue. 我似乎仍然遇到速度问题。 Code works when I step through it in Chrome, but database doesn't get updated properly when I just run it. 当我在Chrome中单步执行代码时,代码就可以工作,但是当我运行它时,数据库无法正确更新。

Don't unbind it, just submit it. 不要取消绑定,只需提交即可。

// $(this).unbind('submit').submit();
this.submit();

though, I wonder why you are preventing the submit in the first place if you're just going to submit it anyway. 但是,我想知道为什么如果您仍然要提交,那么为什么首先要阻止提交。

(function ($) {
$(function () {
    eventTracking();
});

function eventTracking() {
    $("[data-formtracking]").bind('submit', function (e) {
        //e.preventDefault();
        var eventName = $(this).data("formtracking");
        rt.trackEvent(eventName);
        //$(this).unbind('submit').submit();
    });
}})(jQuery);

So not sure if this is the prettiest in the world, but it now works....which is a huge improvment. 所以不确定这是否是世界上最漂亮的,但是现在它可以工作了。。。。

    $("[data-formtracking]").bind('submit', function (e) {
        e.preventDefault();
        form = this;
        var eventName = $(this).data("formtracking");
        rt.trackEvent(eventName);

        setTimeout(function() {
            form.submit();
        }, 100);
    });

Basically, the issue seemed to be with unbinding and resubmit using jquery, so we worked around that and used javascript's submit instead. 基本上,问题似乎与使用jquery解除绑定和重新提交有关,因此我们解决了该问题,改而使用javascript的Submit。

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

相关问题 提交按钮不适用于Chrome或Firefox,但适用于Internet Explorer - The Submit Button doesn't work in Chrome or Firefox, but Works in Internet Explorer 表单提交在Chrome中不起作用,即 - Forms submit doesn't work in Chrome, ie 表单提交按钮在 Firefox 中不起作用,但在 Chrome 或 IE 中有效 - Form submit button does not work in Firefox but works in Chrome or IE Ajax提交可在Chrome / Safari中运行,但不能在Firefox / IE中运行 - Ajax submit works in Chrome/Safari but not Firefox/IE [0].submit 不适用于 IE 和 Firefox,在 Chrome 中完美运行 - [0].submit not working in IE and Firefox, works perfect in Chrome Javascript不适用于Chrome和Firefox,但适用于IE - Javascript doesn't work on Chrome and Firefox, but works on IE EmberData unloadAll在IE8上不起作用。 适用于Chrome / FireFox - EmberData unloadAll doesn't work on IE8. Works on Chrome/FireFox JavaScript时钟可在Chrome上使用,但不适用于Firefox或IE - JavaScript clock works on Chrome but doesn't work with Firefox or IE JavaScript代码在chrome上有效,在IE和Firefox上无效 - JavaScript code works on chrome, doesn't work on IE and firefox Javascript 自动完成功能适用于 IE 和 Chrome,不适用于 Firefox - Javascript autocomplete works in IE and Chrome, doesn't work in Firefox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM