JQuery消息代码片段适用于Chrome / Firefox,而不适用于IE8

时间:2013-11-07 20:09:42

标签: jquery google-chrome firefox internet-explorer-8

我在这里使用一些javascript遇到了一些困难。这段代码似乎在Firefox和Chrome中正常工作(如果搜索词长度小于2个字符,则显示错误消息),但对于我的生活,我无法弄清楚为什么这不会呈现错误消息IE8。

我尝试过更改以下内容: (见:jQuery issue in Internet Explorer 8

认为它可能是一个jquery的细微差别,但我开始认为代码可能有问题,或者它可能必须有一些IE8特定的逻辑,但我没有想法。 JQuery实际上适用于其他方法,它只是这一段代码似乎没有在IE上呈现错误消息。

$(document).ready(function() {
$('#countrySelectionSubmitButton').on('click',function(){   
    $.cookie("locale","en_US");
});

$('#countrySelectionCancelButton').on('click',function(){
    alert($.cookie("locale"));
});

$('#countrySelectionDisplayLocale').text($.cookie("locale"));


var showError = false;
if ($( "#mainSearch" ).length > 0) {
    $( "#mainSearch" ).autocomplete({
        minLength: 0,
        source: function(request, response) {
            var term = $("#mainSearch").val();
            if ( term.length < 2 || term === $("#mainSearch").attr('title')) {
                response([SiteSearch.ErrorMessage]);
                if ( !showError) {
                    $( "#mainSearch" ).autocomplete('close');
                }
                showError = false;
            } else {
                if (term.length >= 3) {
                    $( "#mainSearch" ).autocomplete('enable');
                    $.ajax({
                        url: SiteSearch.LookAheadURL +"?term=" + $('#mainSearch').val() + "&countryCode=" + SiteSearch.CountryCode,
                        type: 'GET',
                        //dataType: 'jsonp',
                        //jsonp: 'callback', 
                        //jsonpCallback: 'autocomplete_callback',
                        error: function(xhr, status, error) {
                            throw new Error ("Autocomplete Service Error");
                        },
                        success: function(data) {
                            //console.log('ajax success');
                            var dataset = [];

                             $.each(data.suggestions, function(i,v) {
                                dataset.push($('<div/>').html(v.value).text()); // Original
                              });
                             response(dataset);
                        }
                    });
                }
            }
        },
        select: function (event, ui) {
            if (ui.item.value === SiteSearch.ErrorMessage) {
                event.preventDefault();
            } else {
                $(this).val(ui.item.value);
                $('#sitesearchform').submit();
            }
        }// Point of the list (DDL ErrorMessage being created / populated) IE doesn't render correctly.
    }).data("autocomplete")._renderItem = function(ul, item) {
        var re = new RegExp("(" + RegExp.escape(this.term) + ")", 'gi') ;
        var match = item.label;
        if (item.value !== SiteSearch.ErrorMessage) {
            match = item.label.replace(re, "<span class='autocompleteMatch'>$1</span>");
        }
        return $( "<li></li>" )
            .data("item.autocomplete", item)
            .append( "<a>" + match + "</a>" )
            .appendTo(ul);
    };
} else {
    throw new Error("Could not find $('#mainSearch')");
}      
$("#sitesearchform").submit(function(){
    var term = $("#mainSearch").val();
    if ( term.length < 2 || term === $("#mainSearch").attr('title')) {
        showError = true;            
        $( "#mainSearch" ).autocomplete('search');
        return false;
    }
    return true;
});

编辑:我还注释掉了console.log行

任何想法或想法将不胜感激。提前谢谢大家。

  • 器Rr

1 个答案:

答案 0 :(得分:0)

我实际上最终修改了方法,以显示标签内放置的错误消息。

$("#sitesearchform").submit(function(){
var term = $("#mainSearch").val();
if ( term.length < 2 || term === $("#mainSearch").attr('title')) {
    showError = true;            
    $( "#mainSearch" ).autocomplete('search');
    $('#headerSearch > ul').show();
    return false;
}
return true;
});
相关问题