Javascript:如果确认从不同函数返回true,则触发

Javascript:如果确认从不同函数返回true,则触发,javascript,php,jquery,Javascript,Php,Jquery,是否可以将我的确认条件触发到其他功能 myphp.php <input type="button" id="button1" class"button2"> <script> $('#button1').on('click', function(){ if(confirm("Are you sure?")){ //my stuff }else{ return false; } }); $('.button2).on('click', function(){

是否可以将我的确认条件触发到其他功能

myphp.php

<input type="button" id="button1" class"button2">

<script>
$('#button1').on('click', function(){
if(confirm("Are you sure?")){
   //my stuff
}else{
   return false;
}
});

$('.button2).on('click', function(){
    //if the confirm condition from first function return true
    //i need to fire here as well without doing another if(confirm)
});
</script>

我建议您通过将希望在两个位置使用的逻辑放在一个两个位置都可以调用的函数中来模块化代码:

// The function doing the thing
function doTheThing(/*...receive arguments here if needed...*/) {
  // ...
}
$('#button1').on('click', function(){
  if(confirm("Are you sure?")){
     doTheThing(/*...pass arguments here if needed...*/);
  }else{
     return false;
  }
});

$('.button2').on('click', function(){
  //if the confirm condition from first function return true
  //i need to fire here as well without doing another if(confirm)
  doTheThing(/*...pass arguments here if needed...*/);
});
旁注:我已经在脚本的顶层展示了它,但是如果您还没有,并且您的问题中没有,我建议您将所有代码放在一个立即调用的作用域函数中,以避免全局:

(function() {
  // The function doing the thing
  function doTheThing(/*...receive arguments here if needed...*/) {
    // ...
  }
  $('#button1').on('click', function(){
    if(confirm("Are you sure?")){
       doTheThing(/*...pass arguments here if needed...*/);
    }else{
       return false;
    }
  });

  $('.button2').on('click', function(){
    //if the confirm condition from first function return true
    //i need to fire here as well without doing another if(confirm)
    doTheThing(/*...pass arguments here if needed...*/);
  });
})();

我建议您通过将希望在两个位置使用的逻辑放在一个两个位置都可以调用的函数中来模块化代码:

// The function doing the thing
function doTheThing(/*...receive arguments here if needed...*/) {
  // ...
}
$('#button1').on('click', function(){
  if(confirm("Are you sure?")){
     doTheThing(/*...pass arguments here if needed...*/);
  }else{
     return false;
  }
});

$('.button2').on('click', function(){
  //if the confirm condition from first function return true
  //i need to fire here as well without doing another if(confirm)
  doTheThing(/*...pass arguments here if needed...*/);
});
旁注:我已经在脚本的顶层展示了它,但是如果您还没有,并且您的问题中没有,我建议您将所有代码放在一个立即调用的作用域函数中,以避免全局:

(function() {
  // The function doing the thing
  function doTheThing(/*...receive arguments here if needed...*/) {
    // ...
  }
  $('#button1').on('click', function(){
    if(confirm("Are you sure?")){
       doTheThing(/*...pass arguments here if needed...*/);
    }else{
       return false;
    }
  });

  $('.button2').on('click', function(){
    //if the confirm condition from first function return true
    //i need to fire here as well without doing another if(confirm)
    doTheThing(/*...pass arguments here if needed...*/);
  });
})();

如果为true{$'.button2.click}@u_mulder:那就是无法维护的意大利面-另一个可以从两个事件处理程序调用的命名函数如何?为什么需要为同一个按钮注册两个单击事件?你不能在一个函数中解决所有问题吗?如果为真{$'.button2.click}@u_mulder:那就是无法维护的意大利面-另一个可以从两个事件处理程序调用的命名函数如何?为什么需要为同一个按钮注册两个单击事件?你不能在一个函数中解决所有问题吗?