简体   繁体   English

jQuery确认不起作用第一次点击

[英]jquery confirm not working on first click

My jquery is as follows: 我的jQuery如下:

function confirmRxAction(tNum) {
  var aUrl = "";
  alert(tNum);
  if (confirm("Are you sure you want to close?")) {
    aUrl = "${pageContext.request.contextPath}/tax/executeoneTimeEx.html?tNum=" + tNum;
    $("#aRx").click();
  }

  $(document).ready(function() {
    $("#aRx").click(function() {

      xhr = $.ajax({
        type: "POST",
        url: aUrl,
        dataType: "text",
        error: function(XMLHttpRequest, errorThrown) {},
        success: function(ajaxResult, testStatus) {

          if (ajaxResult.length > 0)
            alert(ajaxResult);
          else
            afterUpdate();

        }
      });

      return false;
    });
  });
}

When the hyperlink is clicked the fucntion is called and pops up this confirm message with yes or no. 单击超链接后,将调用功能,并以是或否弹出此确认消息。 When i select yes for the first time control doesnt goto controller. 当我第一次选择是时,控制不会转到控制器。 The confirm popup closes but no action is taken. 确认弹出窗口关闭,但不执行任何操作。 So i go click hyperlink for the second time and everything works fine. 所以我第二次单击超链接,一切正常。 What is wrong that prevents the action to be called on first click? 发生什么错误导致无法在首次单击时调用该操作?

Your $(document).ready function is inside your confirm function, so the click event isn't attached until confirm function is run the first time. $(document).ready函数位于确认函数内部,因此在第一次运行确认函数之前,不会附加click事件。 Try moving the $(document).ready function outside of the confirm function. 尝试将$(document).ready函数移至Confirm函数之外。

Don't know why you writen your code like this, but you should use it like below:: 不知道为什么要这样编写代码,但应按以下方式使用它:

function confirmRxAction(tNum){
    var aUrl = "";
    alert(tNum);
    if(confirm("Are you sure you want to close?")) {
            aUrl = "${pageContext.request.contextPath}/tax/executeoneTimeEx.html?tNum=" + tNum;
            xhr = $.ajax({type: "POST", 
                url: aUrl,  
                dataType: "text",
                error: function(XMLHttpRequest, errorThrown){                              
                }, 
                success: function(ajaxResult, testStatus){

                    if(ajaxResult.length>0)
                        alert(ajaxResult);
                    else
                        afterUpdate();

                }
            });
        }
}

Just replace your function with my function. 只需将您的功能替换为我的功能即可。

I would suggest setup the click on document ready instead. 我建议改为准备好单击文档。 And add the confirm in the click() like in the snippet example. 像片段示例一样,在click()中添加确认。

 function afterUpdate() { alert("after update"); } $(document).ready(function() { $("#aRx").click(function(event) { event.preventDefault(); if (confirm("Are you sure you want to close?")) { xhr = $.ajax({ type: "GET", url: "http://services.groupkt.com/country/get/all", dataType: "json", error: function(XMLHttpRequest, errorThrown) { alert("error" + errorThrown); }, success: function(data) { if (data != null) alert(data); else afterUpdate(); } }); } return false; }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a id="aRx" href="#">Click Here</a> 

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

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