简体   繁体   English

在jQuery Mobile和IE上单击两次事件触发

[英]Click Event Fired Twice on jQuery Mobile and IE

I have a jQuery Mobile App that relies on a popup to run a Time & Attendance system. 我有一个jQuery Mobile App,它依赖于弹出窗口来运行考勤系统。 The system works fine except for IE versions 9+ on Windows 8+. 该系统运行正常,但Windows 8+上的IE 9+版本除外。

When the button to check in/out at a client is pressed, a POST request is sent to a handler page to manage the request (ie validate data, enter into database) before being forwarded to the relevant client entry. 当按下用于在客户端签入/签出的按钮时,POST请求将被发送到处理程序页面以管理该请求(即,验证数据,进入数据库),然后转发到相关的客户端条目。

What appears to be happening is the button event is being fired twice. 似乎正在发生的是按钮事件被触发了两次。 The first time is flagged as "Aborted" (as seen in Result column of the F12 Developer Tools) before it is called again (I would post images, but don't have sufficient reputation). 第一次被标记为“已终止”(如F12开发人员工具的“ Result列中所示),然后再次被调用(我会发布图像,但信誉不佳)。 However it seems the call is making it to the handler page, despite being supposedly aborted, as there are TWO entries appearing in the database. 但是,尽管该调用被中止,但似乎正在进入处理程序页面,因为数据库中出现了两个条目。

The code for the button is as follows: 该按钮的代码如下:

<button data-inline="true" data-mini="true" data-theme="b" data-ajax="false" id="submitBtn" onClick="this.form.submit(); this.disabled=true;">Check Out / Split Visit</button>

I deliberately disable the button so users can't click on the button multiple times. 我故意禁用该按钮,以使用户无法多次单击该按钮。 I have tried different ways of generating the button, including using a href and setting the data-role to button and making the button a submit button - the same thing occurs. 我尝试了多种生成按钮的方法,包括使用a href和将data-role设置为button并使按钮成为submit按钮-发生相同的事情。 There is no other JavaScript running on the button click as it was playing up with some mobile devices (user validation is done on the handler page). 由于某些移动设备正在播放按钮,因此单击按钮上没有其他JavaScript运行(用户验证在处理程序页面上完成)。

I can force the page to render as an older version of IE by setting the X-UA-Compatible to be IE=7 , but this makes the app awful to look at and I'm not sure about the security aspects of forcing IE to handle the page as an earlier version. 我可以通过将X-UA-Compatible设置为IE=7来强制将页面呈现为IE的较早版本,但这使应用程序看起来很糟糕,而且我不确定强制IE强制执行将页面作为早期版本处理。

I'm using jQuery Mobile 1.3.2 and jQuery 1.10.2 as the newer versions don't have some features that are core to our application. 我使用的是jQuery Mobile 1.3.2和jQuery 1.10.2,因为较新的版本没有某些功能是我们应用程序的核心。 The backend is written in PHP. 后端是用PHP编写的。 The fault has been duplicated on Windows Phone 8, a Surface 3 and Windows 8.1 Desktop. 该错误已在Windows Phone 8,Surface 3和Windows 8.1桌面上重复出现。

Popup Code: 弹出代码:

<div data-role="popup" id="checkInOut" data-theme="b" data-dismissible="false" data-overlay-theme="b" class="ui-content" style="max-width:340px; padding-bottom:2em;">
        <h3 id="checkTitle">Check-In At Client</h3>
        <form action="checkInOutHandler.php" method="post" name="checkInOutForm" id="checkInOutForm" data-ajax="false">
            <input type="hidden" name="TAClientID" id="TAClientID" />
            <input type="hidden" name="actionType" id="actionType" value="In" />
            <label for="useCurrentTime">
                Use Current Time
            </label>
            <input id="useCurrentTime" name="useCurrentTime" type="checkbox" class="useCurrentTime" data-mini="true" checked="checked" />

            <!-- Hidden Div for the manual entry of time !-->
            <div id="customTimeDiv" style="display: none;">
                <label for="customTime">
                    Enter the time you wish to use (in 24 Hour format hh:mm)
                </label>
                <input id="customTime" name="customTime" type="time" placeholder="Enter Time (hh:mm)" data-mini="true" />

                <label for="customReason">
                    You MUST select a reason for not using the current time
                </label>
                <select name="customReason" id="customReason" data-theme="b" data-icon="arrow-d" data-inline="true" data-mini="true">
                    <option value="" selected>Please select a reason...</option>
                    <option value='937'>No reception</option><option value='938'>Forgot</option><option value='943'>Phone flat / not working</option><option value='944'>Problem with portal</option><option value='949'>Split Visit</option><option value='964'>Incorrect Client Selected</option><option value='963'>Incorrect Service Selected</option><option value='945'>Other (Payroll may call to confirm)</option>                </select> 
            </div>
            <!-- End Hidden Div !-->
            <br />
            <input type="hidden" id="travelTime" name="travelTime" data-mini="true" data-inline="true" value="0" />
            <button data-inline="true" data-mini="true" data-theme="b" data-ajax="false" id="submitBtn" onClick="this.form.submit(); this.disabled=true;">Check In</button>
            <a href="#" data-role="button" data-rel="back" data-inline="true" data-mini="true">Cancel</a>
        </form>
    </div>

Header Code: 标题代码:

<!DOCTYPE html>
<html>
    <head> 
        <title>SMNCC App</title> 
        <meta http-equiv="X-UA-Compatible" content="IE=Edge">
        <meta name="viewport" content="width=device-width, initial-scale=1"> 
        <link rel="shortcut icon" href="https://portal.suemann.com.au/favicon.ico" />

        <!-- <script type="text/javascript" src="./js/timeTrack.js"></script>!-->
        <script type="text/javascript" src="./js/readNewsItem.js"></script>

        <link rel="stylesheet" href="./jqueryMobile/jquery.mobile.min.css" /> <!-- Original File: jquery.mobile-1.3.2.min.css !-->
        <link rel="stylesheet" href="./themes/hartijaPrint.css" type="text/css" media="print"> <!-- Hartija print CSS framework !-->
        <script type="text/javascript" src="./jqueryMobile/jquery.min.js"></script> <!-- Original File: jquery-1.10.2.min.js !-->

        <script type="text/javascript" src="./jqueryMobile/disableAjax.js"></script>

        <script type="text/javascript" src="./jqueryMobile/jquery.mobile.min.js"></script> <!-- Original File: jquery.mobile-1.3.2.min.js !-->      

    </head> 
    <body

I'm not running Windows 8 so I can't test it, but you can check to see if the button has already been pressed using jQuery. 我没有运行Windows 8,所以无法对其进行测试,但是您可以使用jQuery检查按钮是否已被按下。

Eg: Html code: 例如:HTML代码:

<button id="mybutton">Click me!</button>

jQuery: jQuery的:

var alreadySent = false;
$(document).ready(function() {
  $("#myButton").click(function(event) {
    event.preventDefault();
    if (!alreadySent) {
      sendPostRequestOrWhatever();
      alreadySent = true;
    }
    $(this).prop("disabled", true);
  });
});

Of course, this assumes that when you say the button fires twice you mean that the click event is running twice. 当然,这假设当您说按钮触发两次时,意味着单击事件运行了两次。 If the click event is only firing once but the send request is firing twice, then the code won't work. 如果click事件仅触发一次,但发送请求却触发两次,则该代码将无法工作。

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

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