:按钮选择器在火狐和IE中不起作用

:button selector not working in firefox and ie

本文关键字:IE 不起作用 火狐 按钮 选择器      更新时间:2023-09-26

请帮助我:按钮选择器用于取消绑定我的Spring MVC项目中所有按钮的事件在chrome中工作正常,但在Firefox和Internet Explorer中则无法正常工作。

我的任务是从项目中的所有按钮中取消绑定事件。 代码如下:

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 中则不行。请帮助我更正此代码或建议我使用其他方法从所有按钮取消绑定事件。提前非常感谢

嗨,我刚刚遇到了实际问题.问题是由于输入元素的类型是按钮,这会产生问题。如果我们将按钮的输入类型设置为"提交",则在Firefox和其他按钮中工作正常。但是如果按钮的输入类型设置为 "按钮" ,则它不起作用。我的意思是这个.请帮助我。如何解决这个问题。提前谢谢。

哦,现在我明白你的意思了。问题是,您已将禁用按钮功能分配给按钮(在 HTML 中)...然后用Java脚本(在文档准备好之后)分配了另一个函数[jQuery(":button").on('click', ...)...取消绑定() ]

问题出在调用顺序上:首先禁用按钮,然后取消绑定()。并且所需的行为只能通过相反的顺序来实现。

我的建议是要么使用 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>

它在火狐中工作。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
      <title></title>
      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
     <script>
    $(document).ready(function(){
      $("button").click(function(){
        alert("Hello");
      });
    });
    </script>
      </head>
      <body>
       <button> hello</button>
      </body>
    </html>