Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如果满足条件,则显示警报,几乎与onbeforeunload相同_Jquery_Onbeforeunload - Fatal编程技术网

Jquery 如果满足条件,则显示警报,几乎与onbeforeunload相同

Jquery 如果满足条件,则显示警报,几乎与onbeforeunload相同,jquery,onbeforeunload,Jquery,Onbeforeunload,我有一张分为5步的表格。当前,如果您在步骤2-4中刷新/退出/关闭等,它将通过window.onbefore返回一条消息,在那里卸载无问题 window.onbeforeunload = function () { if ( $("#step1").is(":visible") || $("#step2").is(":visible") || $("#step3").is(":visible") ) { return 'Using the

我有一张分为5步的表格。当前,如果您在步骤2-4中刷新/退出/关闭等,它将通过window.onbefore返回一条消息,在那里卸载无问题

window.onbeforeunload = function () {
            if ( $("#step1").is(":visible") || $("#step2").is(":visible") || $("#step3").is(":visible") ) {
                return 'Using the browsers back, refresh or close button will cause you to lose all form data. Please use the Next and Back buttons on the form.';
            }
        }
现在,如果复选框“A”被选中而复选框“B”未被选中,我需要在步骤0(第一步)中添加相同或类似类型的行为。这可以工作,但不能与另一个.onbeforeunload函数结合使用

window.onbeforeunload = function () {
            if ( $("#A").is(":checked") && $("#B").is(":not(:checked)") ) {
                return 'Message here. Do you wish to continue?';
            }
        }

我怎样才能把这两个链子连在一起?使用if,else语句似乎不起作用。

您可以将它们组合到一个函数中,如下所示:

window.onbeforeunload = function () {
  if ( $("#A").is(":checked") && !$("#B").is(":checked") ) {
      return 'Message here. Do you wish to continue?';
  }
  if ( $("#step1, #step2, #step3").filter(":visible").length) {
      return 'Using the browsers back, refresh or close button will cause you to lose all form data. Please use the Next and Back buttons on the form.';
  }
}
if ($('#step0').is(':visible') && $("#A").is(":checked") && !$("#B").is(":checked") ) {
我简化了您的另一个选择器,以获取所有元素,仅获取一个元素,并查看是否有(>0),这只是一种更易于维护的方法

此外,您可能还需要在第一个
中添加一个附加检查,如果
,请确保您已进入该步骤,如下所示:

window.onbeforeunload = function () {
  if ( $("#A").is(":checked") && !$("#B").is(":checked") ) {
      return 'Message here. Do you wish to continue?';
  }
  if ( $("#step1, #step2, #step3").filter(":visible").length) {
      return 'Using the browsers back, refresh or close button will cause you to lose all form data. Please use the Next and Back buttons on the form.';
  }
}
if ($('#step0').is(':visible') && $("#A").is(":checked") && !$("#B").is(":checked") ) {

这与您添加它们的方式有关吗?乍一看,一个赋值将覆盖另一个赋值,但是如果使用jquery.bind(),那么我认为它将正确处理添加多个事件处理程序的问题。。。(作为评论,因为我不确定是否可以作为答案)。嘿,尼克,这是页面,它没有按预期工作。也许我不需要第0步的onbeforeunload?当我点击“下一步”时,它不会被触发,只有当我刷新时才会触发。这应该是验证吗?如果是这样,如果需要选择,如何避免陷入循环?如果“#A”是一个警告,那么选中“#B”并不是绝对的,只是一个警告。@Dirty-在你的情况下,听起来你想在第一个
next
按钮上进行A/B检查,对吗?是的,出于某种原因,这不起作用。。。$(“#step0Next”).one('click',function(){if($(“#A”).is(“:checked”)&&$(“#B”).is(“:not(:checked)”){alert(“foo”);});@Dirty-可能是因为这些元素是动态创建的,请确保代码在formToWizard代码之后运行,或者像这样运行:
live
:live(“#step0Next”).live('click',function(){if($(“#A”).is(“:checked”)&(&$(“#B”).is(:not checked)”){alert('foo”)$(this.die('click')})
@Nick-这种方式很有效,但它并没有阻止表单进入第2步,这是需要的。我怎样才能让它阻止台阶向前移动?还有,为什么“.one”工作,使用“.live”似乎不是正确的选择,现在我明白了。$(this.die('.click')做什么?