javascript - :button selector not working in firefox and ie

标签 javascript jquery css-selectors onbeforeunload unbind

请帮帮我 :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/

相关文章:

javascript - 在 JQuery 中构建 div 结构并追加到另一个 Div

jquery - 在 jquery 中移动带有文本的 div + array,IE 行为不端

jquery - fancybox onClosed 回调不起作用

jquery - 单个 li 元素 - 通过 jQuery 的特定背景

javascript - Jquery中如何将局部变量赋值给普通变量

javascript - 使用 onClick 事件处理程序时不断调用自身

css - 是否可以组合 css3 伪类 :after and :lastchild?

html - 如何使用 css nth-child(-n+3) 仅显示列表中的前三个图像

javascript - JavaScript 中最好的 "pass"命令?

javascript - 为什么在包含字符串的数组上运行 jquery each() 会将每个字符串分解为单个字符的数组?