简体   繁体   English

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

[英]:button selector not working in firefox and ie

Kindly help me :button selector to unbind the events from all the buttons in my Spring MVC project is working fine in chrome but not in firefox and internet explorer. 请帮助我:button选择器解除我的Spring MVC项目中所有按钮的绑定,在chrome中工作正常,但在Firefox和Internet Explorer中无法正常工作。

My task is to unbind the event from all the buttons in the project . 我的任务是从项目中的所有按钮取消绑定事件。 The code is as follows : 代码如下:

 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') }); }); 

Now this is working fine in chrome but not in ie and firefox. 现在,这在chrome中可以正常工作,但在ie和firefox中却不能。 Please help me to correct this code or suggest me some other way to unbind the event from all buttons . 请帮助我纠正此代码,或建议我以其他方式取消所有按钮的绑定。 Many Thanks in advance 提前谢谢了

Hi I just got the actual problem . 嗨,我只是遇到了实际问题。 The problem is because of the type of the input element that is button , which is creating problem. 问题是由于输入元素的类型为button,这导致了问题。 If we set the input type of the button to "submit" it works fine in Firefox and others. 如果我们将按钮的输入类型设置为“提交”,则在Firefox和其他浏览器中可以正常工作。 But if the input type of the button is set to "button" , then it does not work. 但是,如果按钮的输入类型设置为“ button”,则它将不起作用。 I mean this to . 我的意思是。 Kindly help me. 请帮助我。 How to fix this. 如何解决这个问题。 Thanks in advance. 提前致谢。

oh, now I see what you mean. 哦,现在我明白你的意思了。 The Problem was, that you have been assigned the disableButton -function to the button (within of HTML)... and THEN with Java Script (after the document were ready) assigned another function [ jQuery(":button").on('click', ...)... unbind() ] 问题是,您已经为按钮分配了disableButton -function (在HTML内)...,然后使用Java脚本(在文档准备好之后)给THEN分配了另一个函数[ jQuery(“:button”)。on( 'click',...)... unbind() ]

The problem lies in the call-order: First disableButton and than unbind() . 问题出在调用顺序上:首先是disableButton,然后是unbind() And the desired behavior is achievable only with reverse order. 并且只有逆序才能实现所需的行为。

My proposition were either to use submit (like you said before, with almost no js) or to add j$(this).unbind("beforeunload"); 我的建议是要么使用Submit(就像您之前所说的,几乎没有js),要么添加j $(this).unbind(“ beforeunload”); to disableButton() (it works in firefox and ie too :-) ): disableButton() (它在firefox中也有效:-)):

<!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>

its working in firefox.............. 它在Firefox中工作。

<!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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM