如何使用else {}函数弹出jQuery UI Dialog?

问题描述 投票:-1回答:3

我正在使用jQuery UI对话框。此处的代码会在单击HTML按钮时弹出对话框。

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
   <script>
  $( function() {
    $( "#dialog" ).dialog({
      autoOpen: false,
      show: {
        effect: "blind",
        duration: 1000
      },
      hide: {
        effect: "explode",
        duration: 1000
      }
    });

    $( "#opener" ).on( "click", function() {
  $( "#dialog" ).dialog( "open" );
});
  } );
  </script>
</head>
<body>

<div id="dialog" title="Basic dialog">
  <p>This is an animated dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>
</div>

<button id="opener">Open Dialog</button>


</body>
</html>

这很好,但我有一个if else函数:

  if(form.pswrd.value === "password")
  {
  window.open('page.html')/*opens the target page while Id & password matches*/
  }
  else
  {
//jQuery Dialog opens HERE
  }
  }

底部的else函数是我想要触发jQuery UI对话框的。如何让else打开jQuery对话框?我已经尝试过将这部分脚本包含在我的else中,但无济于事:

$( "#opener" ).on( "click", function() {
  $( "#dialog" ).dialog( "open" );
});
  } );
javascript jquery html css jquery-ui
3个回答
1
投票

你的代码看起来很好,你只需要触发在else子句中打开的对话框:

<div id="dialog" title="Basic dialog">
  <p>This is an animated dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>
</div>

<button id="opener">Open Dialog</button>

<script>
$(function() {
  $("#dialog").dialog({
    autoOpen: false,
    show: {
      effect: "blind",
      duration: 1000
    },
    hide: {
      effect: "explode",
      duration: 1000
    }
  });

  $("#opener").on("click", function() {
    if (form.pswrd.value === "password") {
      window.open("page.html");
    } else {
      $("#dialog").dialog("open");
    }
  });
});
</script>

0
投票

您不需要单击侦听器手动打开它

做就是了:

else { 
   $( "#dialog" ).dialog( "open" );
}

-1
投票
$( "#opener" ).on( "click", function() {
  if(form.pswrd.value === "password") {
      window.open('page.html'); /*opens the target page while Id & password matches*/
    } else {
      $( "#dialog" ).dialog( "open" );
    }
  }
});
© www.soinside.com 2019 - 2024. All rights reserved.