请帮帮我 :button 选择器取消绑定(bind)我的 Spring MVC 项目中所有按钮的事件在 chrome 中工作正常但在 firefox 和 internet explorer 中不工作。
我的任务是解除事件与项目中所有按钮的绑定(bind)。代码如下:
var j$ = jQuery.noConflict();
j$(document).ready(function() {
jQuery(window).bind(
"beforeunload",
function() {
return confirm("do you want to exit?" , "true");
}
);
jQuery(":button").on('click', function(){
jQuery(window).unbind('beforeunload')
});
jQuery('a').on('click', function(){
jQuery(window).unbind('beforeunload')
});
jQuery(window).bind('keydown keyup', function(e) {
if(e.which === 116) {
jQuery(window).unbind('beforeunload');
}
if(e.which === 82 && e.ctrlKey) {
jQuery(window).unbind('beforeunload');
}
});
jQuery(window).mousedown(function(e){
if( e.button == 2 ) {
jQuery(window).unbind('beforeunload');
}
else{
jQuery(window).bind('beforeunload');
}
});
jQuery("form").submit(function(){
jQuery(window).unbind('beforeunload')
});
});
现在这在 chrome 中可以正常工作,但在 ie 和 firefox 中却不行。请帮助我更正此代码或建议我使用其他方法解除所有按钮事件的绑定(bind)。 非常感谢
您好,我刚遇到实际问题。问题是由于输入元素的类型 button 造成了问题。如果我们将按钮的输入类型设置为“提交”,它在 Firefox 和其他软件中工作正常。但是如果按钮的输入类型设置为 "button",那么它就不起作用了。我的意思是。请帮助我。如何解决这个问题。提前致谢。
最佳答案
哦,现在我明白你的意思了。 问题是,您已将 disableButton 函数分配给按钮(在 HTML 中)...然后使用 Java Script(在文档准备好之后)分配了另一个函数 [ jQuery(":button").on('click', ...)... unbind() ]
问题出在调用顺序上: 首先是 disableButton,然后是 unbind()。并且只有通过相反的顺序才能实现所需的行为。
我的建议是要么使用 submit(就像你之前说的,几乎没有 js)要么添加 j$(this).unbind("beforeunload"); 到 disableButton( )(它适用于 firefox 和 ie 也 :-)):
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=windows-1252">
<script type="text/javascript" src="demo-Dateien/jquery.js"></script>
<script type="text/javascript">
function disableButton() {
j$(this).unbind("beforeunload");
document.form.method="GET";
document.form.but.disabled = true;
document.form.submit();
}
var j$ = jQuery.noConflict();
j$(document).ready(function() {
j$(window).bind(
"beforeunload",
function() {
return confirm("do you want to exit?" , "true");
}
);
});
</script>
</head>
<body>
<form name="form" action="process.jsp" method="post">
<label>First Name</label>
<input name="fname" type="text">
<br>
<label>Last Name</label>
<input name="lname" type="text">
<br>
<input class="Button" name="but" value="Next" onclick="javascript:disableButton()" type="button">
</form>
</body>
</html>
关于javascript - :button selector not working in firefox and ie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35126516/