如果在javascript中弹出警报

时间:2018-04-05 13:31:16

标签: javascript jquery

我有这段代码

  <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.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();
  } );
  </script>


<div id="dialog" title="Basic dialog">
  <p>Item not found</p>
</div>


<script>
var number = 1;

if (number == 1) {
    alert('In here');
}
</script>

但是我想在if中运行jquery运行并删除警报。我已经尝试过innerHTML,但没有运气这是可以做到的事情。

2 个答案:

答案 0 :(得分:2)

删除行alert('In here');并将行$( "#dialog" ).dialog();放在if语句中。然后应该将该代码块放在jQuery文档加载事件中:

<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.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() {
      var number = 1;

      if (number === 1) {
          $( "#dialog" ).dialog();
      }
  });
  </script>

<div id="dialog" title="Basic dialog">
  <p>Item not found</p>
</div>

修改

或者,只是脚本块中的代码(不使用jQuery加载事件):

<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.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>

<div id="dialog" title="Basic dialog">
  <p>Item not found</p>
</div>

<script>
var number = 1;

if (number === 1) {
    $( "#dialog" ).dialog();
}
</script>

答案 1 :(得分:0)

假设我理解你的要求:jQuery dialog对象包括&#34; close&#34;方法,如果要在没有用户干预的情况下关闭对话框,可以使用该方法:

http://api.jqueryui.com/dialog/#method-close

&#13;
&#13;
$("#dialog").dialog();

$('#hideit').on('click', function() { // button added for demo purposes
  $('#dialog').dialog("close") // This is what you would put in your "IF" statement
});
&#13;
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.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>


<div id="dialog" title="Basic dialog">
  <p>Item not found</p>
</div>

<button id="hideit">hide</button>
&#13;
&#13;
&#13;