第一次点击按钮显示警报,第二次点击执行代码

Click on button first time show alert, click second time execute code

我想向用户显示,当他们第一次单击按钮时,当在日期字段中选择了一个位于当前日期之前的值时会发出警报。当他们出于充分的理由坚持这个选择时,我希望他们给他们第二次机会点击同一个按钮,然后必须提交该值。

点击事件定义在一个函数中:

$("#edit_date_btn").click(function(){
 // do something and save
}

在另一个函数中处理比较。基本代码为:

function edit_date_compare() {
....
if(usersDate < today)
 { //show alert
  return false; // needed for the first click so the input is not submitted
 }

我已经尝试了几种选择,例如对点击功能进行计数(第二次点击 'return true;' 而不是 'return false;'),但似乎很难处理这种情况。任何想法如何使这成功?它可以与 Javascript 或 jQuery.

您对 count on the click function 的想法是正确的。我怀疑您可能难以实施它。您需要一个单独的变量来跟踪点击次数。这是一个有效的 fiddle:http://jsfiddle.net/zwmquxaL/

$(document).ready(function () {
    var clickCount = 0;

  $("#btnSave").click(function () {
    if (clickCount > 0) {
      AlertSave();
    } else {
        AlertFirst();
      clickCount++;
    }
  });
});

function AlertSave() {
  alert("Some code has been executed!");
}

function AlertFirst() {
  alert("Are you sure you want to perform this operation?");
}