自动触发按钮操作,而不是按下它

Auto trigger button action instead pressing it

本文关键字:按钮 操作      更新时间:2023-09-26

我有人做了下面的aspx文件

现在按钮动作需要通过按下"搜索"按钮手动触发,我想实现按钮页面自动触发动作本身而不需要人点击,我该如何实现?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="scripts/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="scripts/CheckBookingApp.min.js"></script>
<script>
    $(document).ready(function () {
        $("#btnSearch").CheckBooking();
    })
</script>
</head>
<body>
<form id="form1" runat="server">
    <div>
        <button id="btnSearch">Search</button>
    </div>
</form>
</body>

只需在domready中调用checkBooking()函数。

你可以拥有它,这样当点击按钮时弹出窗口也会显示出来。

  $(document).ready(function() {
  function checkBooking() {
    alert('Checking booking');
  }
  // when the page loads
  checkBooking();
    
  //when the button is pressed
  $("#btnSearch").on('click', function() {
    checkBooking();
  });
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form>
  <button id="btnSearch">Check booking</button>
</form>

您可以通过javascript $('btnSearch').trigger('click'); 触发"模拟点击"事件

如果你想加入一个时间延迟,你可以使用setTimeout()

在您的示例中:

$(document).ready(function () {
    $("#btnSearch").CheckBooking();
    setTimeout(function() { 
           $("#btnSearch").trigger('click');
        }, 5000 /* 5000ms = 5sec */);
});