Javascript函数在Firefox上不起作用

时间:2017-10-16 12:37:49

标签: javascript jquery html twitter-bootstrap firefox

$("#search-btn-duplicate").click(function(event) {
    $(".search-container .dropdown-menu li").remove();
    $(".search-container .dropdown-menu").append('<li class="loading"><i class="icon-refresh icon-refresh-animate"></i></li>');

   var searchFieldDuplicateContent = $("#search-field-duplicate").val();
   var firstProtocol = $("#protocol").text();
   firstProtocol = firstProtocol.replace(/\s/g, '');

   if (searchFieldDuplicateContent != null){
       if (searchFieldDuplicateContent.length > 2){
           ajaxSearchFieldDuplicateContent(searchFieldDuplicateContent, firstProtocol);
        }
    }
});

function ajaxSearchFieldDuplicateContent(searchFieldDuplicateContent, firstProtocol){
$.ajax({
    method: 'POST',
    url: '/requests/search_for_duplicate',
    data: {
        protocol: searchFieldDuplicateContent,
        first_protocol: firstProtocol
    },
    success: function(data) {
        id = data.substring(0, data.indexOf("_"));
        bindId = data.substring(data.indexOf("|")+1);
        data = data.substring(data.indexOf("_")+1, data.indexOf("|"));
        $(".search-container .dropdown-menu li").remove();
        if (data.length > 0){
            $(".search-container .dropdown-menu").append('<li><a><span id="protocol_to_bind" style="cursor: pointer;" class="span11">'+data+'</span></a></li>');
            $("#protocol_to_bind").click(function(){
                $("#search-field-duplicate").val(data);
                $(".button-submit").click(function(){
                    ajaxRequestBind(bindId);
                });
            });
        }else {
            $(".search-container .dropdown-menu").append('<li><a href="#" > Nenhum Resultado</a></li>');
        }
    }
});
}

function ajaxRequestBind(bindId){
$.ajax({
    method: 'POST',
    url: '/requests/bind/' + id,
    data: {
        bind_id: bindId
    },
});
window.location.href = window.location.href + '?refresh';
}

此功能在Chrome上完美运行,但在Firefox上无效。我认为它可能是下拉列表的引导问题,但无论如何都无法修复它。此外,Firefox会在控制台上为页面上任何位置的每次单击打印警告。 Empty string passed to getElementById().

var searchFieldDuplicateInput = document.getElementById('search-field-duplicate');
searchFieldDuplicateInput.addEventListener('keydown', function(e){
    if(e.keyCode === 13){
        $("#search-btn-duplicate").click();
    }
});

还有一种奇怪的行为,当我第一次按Enter进行搜索时它不起作用并且同样的警告Empty string passed to getElementById().被打印到控制台,但是当我按下第二次时时间,它确实有效。即使我将此代码注释掉,警告仍会显示。这是我的代码中唯一使用getElementById()的地方。

该警告指向该行http://localhost:3000/assets/application.self-5bb11bb64996b58cc9fe309d67b3587d80bbfbb0a0537f7773feae2c6cc4f207.js?body=1

上的该文件elem = document.getElementById( match[ 2 ] );

除此之外,一切都按预期进行,但这部分不起作用ajaxRequestBind(bindId);。页面刷新但rails方法不起作用。

我使用它setTimeout(function () { window.location.href = window.location.href + '?refresh'; }, 2000);必须等待2秒才能刷新页面。如果我在ajax之后立即刷新页面它不起作用。不知道为什么会这样。但是我仍然遇到这种警告弹出的奇怪行为的问题。

1 个答案:

答案 0 :(得分:0)

我使用setTimeout(function () { window.location.href = window.location.href + '?refresh'; }, 2000);在Firefox上工作必须等待2秒才能刷新页面。如果我在ajax之后立即刷新页面它不起作用。不知道为什么会这样。我认为这更像是一种解决方法,而不是解决方案。但是我仍然遇到这种警告弹出的奇怪行为的问题。