帮助防止默认

时间:2010-08-03 18:32:45

标签: jquery

我在表单向导上点击(“#step0Next”)后弹出一个确认对话框。目前,在您按下下一步按钮后会弹出。当你按下THAT步骤(步骤1)上的按钮时,我需要它弹出,而不是当它进入步骤2.当如何在用户点击对话框中的“确定”之前,如何停止按钮的默认操作?

$("#step0Next").live('click', function(event) {
  event.preventDefault();
  if($("#RT").is(":checked") && !$(".ex").is(':checked')) { 
       return confirm ('foo');
       //alert("You have not selected any exchanges for which to receive real time market data from. If you continue, you will only receive real time data for market metrics and ten minute delayed data for everything else. Do you wish to continue?"); 
       $(this).die('click');
  } 
});

2 个答案:

答案 0 :(得分:0)

confirm()没有默认操作。而不是返回它,您需要在检查返回值后阻止默认值。 (我没有完全关注你的问题,但我假设你想在用户点击OK之后继续......不是“停止默认操作”?)

从现在的地方删除event.preventDefault()并执行:

if( ... long if statement ... ) {
    if( confirm("Are you sure? Yadda Yadda") ) {
        // User clicked OK 
    }
    else {
        // User clicked Cancel
        event.preventDefault();
    }
}

当然,如果您确实想要阻止所点击的任何内容的默认操作,即使用户单击了OK,也请将preventDefault调用保留在原来的位置。我提供的代码仍会告诉您他们在确认对话框中点击了哪个按钮。

答案 1 :(得分:0)

这没用。这样做了:

<script type="text/javascript">
    $("#step0Next").live('click', function(event) {
      $('#step1Prev').click();  //go back to step 1
      if($('#RT').is(':checked') && !$('.ex').is(':checked')) {
          if(!confirm("You have not selected any exchanges for which to receive real time market data from. If you continue, you will only receive real time data for market metrics and ten minute delayed data for everything else. Do you wish to continue?")) return;
          $('#step0Next').die('click');
      }
      $(this).triggerHandler('click');
    });
</script>