对输入ie8调用函数无效

call function on input ie8 doesnt work

本文关键字:函数 无效 调用 ie8 输入      更新时间:2023-09-26

我的js文件中有一个函数看起来像(见下文)-我试图在按钮上调用一个函数,但它在IE8中不起作用(得到一个"Engine is undefined"),在FF、Chrome中它工作得很好:HTML:

<button onclick="javascript:Engine.search.globalSearch(window.event);" type="button" class="btn btn-default">Søg</button>

JS:

jQuery(function ($) {
var $w = $(window),
    $body = $('body'),
    resizeTimer,
    tablePx = 768,
    desktopPx = 992,
    winWidth = $w.width();
Engine = {
    search: {
        setupSearch: function () {
            // Add eventlistener for search-submit on Enter keydown
            $("#q").keydown(function (event) {
                if (event.which === 13) {
                    return Engine.search.submitSearch();
                }
            });
            // Datepicker settings 
            // adding datepicker to date fields from the ASK local Search resume
            // Setting op opensourse Bootstrap datepicker
            $('input.addDatePicker').datepicker({
                format: 'dd.mm.yy'
            });
        },
        globalSearch: function (event) {
            if (event.type === 'click' || event.keyCode === 13 || event.which === 13) {
                // (IE-fix) Prevent default form-submit on Enter-press
                event.preventDefault();
                event.cancelBubble = true;
                // Get search-field and placeholder-tekst
                var searchFld = document.getElementById("globalsearch");
                var ph = searchFld.getAttribute("placeholder");
                if (searchFld.value !== "" && searchFld.value !== ph) {
                    // Get sites-searchpage url, combine it with the searchword and open that url.
                    var searchUrl = searchFld.getAttribute("data-searchUrl");
                    var tmpUrl = searchUrl + "?q=" + searchFld.value;
                    window.location = tmpUrl;
                }
                else {
                    return false;
                }
            }
        },
        autoSumitSearch: function () {
            // Submit search if autoSubmit is setup to true by editor (requires variable defined on SC page)
            if (autoSubmit === true) {
                Engine.search.submitSearch();
            }
        },
        submitSearch: function () {
            // Create outer form with search criteria, to be able to submit it outside Sitecores global form-element
            var $ = jQuery;
            var winLoc = String(window.location.href).replace(/[?&]page[=][^&#]?/, "");
            var form = '<form style="display: none;" method="get" action="' + window.location.href + '">' +
                '<input name="q" value="' + $("#q").val() + '">' +
                //'<input name="page" value="' + $("#page").val() + '">' +
                '<input name="ps" value="' + $("#ps").val() + '">' +
                (($("#categ").length > 0) ? '<input name="categ" value="' + $("#categ").val() + '">' : '') +
                (($("#c").length > 0) ? '<input name="c" value="' + $("#c").val() + '">' : '') +
                (($("#ddlCategories").length > 0) ? '<input name="cat" value="' + $("#ddlCategories").val() + '">' : '') +
                (($("#ddlSiteSections").length > 0) ? '<input name="ss" value="' + $("#ddlSiteSections").val() + '">' : '') +
                (($("#df").length > 0) ? '<input name="df" value="' + $("#df").val() + '">' : '') +
                (($("#dt").length > 0) ? '<input name="dt" value="' + $("#dt").val() + '">' : '') +
                '</form>';
            var $form = $(form).appendTo("body");
            setTimeout(function () { $form.submit().remove(); }, 0);
            return false;
        }
    },
};
// Initialize main script-Engine;
Engine.init();
});

可能是由于在函数内部使用了Engine,因此仅限于该函数的作用域。尝试首先全局声明Engine,例如在第一行使用

var Engine;

或者使用在该函数中附加onclick处理程序

$("button.your-button").click(Engine.search.globalSearch);

这也涵盖了使用内联onclick处理程序和事件处理程序可能产生的冲突,该事件处理程序在DOMReady事件中调用的方法中声明。