对输入ie8调用函数无效
call function on input ie8 doesnt work
我的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事件中调用的方法中声明。
相关文章:
- "这个“;函数的作用域无效
- 未捕获的无效值错误:initMap 不是一个函数
- 调用$.get函数时JQuery中出现无效标签错误
- js.erb文件中提供给RegExp构造函数的标志无效
- 对话框函数错误无效函数
- 如何防止JavaScript日期构造函数接受无效日期
- javascript onload函数有效,onresize则无效
- 为什么document.ready中的事件处理程序函数有效但无效;取出后不起作用
- Javascript验证函数总是回复无效
- 从AJAX调用Php函数无效
- 函数调用者无效元素
- 未捕获的无效值错误:initAutocomplete 不是一个函数
- JavaScript 删除函数无效
- 为什么 AJAX 对第一个函数的响应“_blank”有效,但对第二个函数无效
- 为什么某些原型函数覆盖有效而其他函数无效
- Prestashop javascript加载函数有效,但其他函数无效
- 为什么这个记忆实现对匿名函数有效,而对声明函数无效?
- 在window.onload中调用函数无效
- 对输入ie8调用函数无效
- 从C#调用JS函数无效