是否可以在 javascript 中进行两个 .click 方法调用

发布于 2024-07-20 09:54:22 字数 544 浏览 6 评论 0原文

我有以下 javascript 代码:

function ClickButtons() {
   document.getElementById('Button1').click();
   document.getElementById('Button2').click();
}

似乎只有 Button2 被单击。 如果我颠倒语句的顺序,那么似乎只有 Button1 (然后称为 2nd)可以工作。

仅供参考(不认为这会影响此问题,请在此处获取更多信息):按钮单击正在执行 ajax/部分页面更新(它们调用数据服务并在页面上填充数据)

编辑:解决方案 setTimeout 有效,但放置了一个性能下限。 我做了更多的查找,这两个按钮是 asp.net 按钮,位于更新面板内。 如果我按顺序单击它们,它们工作正常,但如果我单击一个,然后快速单击第二个(在第一个完成之前),那么第二个将工作,第一个将失败。 这似乎是更新面板和 asp.net 的问题? 我可以在这方面做些什么来启用上面的 javascript 并避免 setTimeout 选项吗?

I have the following javascript code:

function ClickButtons() {
   document.getElementById('Button1').click();
   document.getElementById('Button2').click();
}

only Button2 seems to be clicked. If I reverse the order of the statements then only Button1 (which would be called 2nd then) seems to work.

FYI (don't think this is impacting this issue, here for futher information): The button clicks are doing ajax/partial page updates (they call data services and populate data on the page)

EDIT: The solution setTimeout works, but puts an lower bound on performance. I've done a bit more looking and the two buttons are asp.net buttons and are inside update panels. If I click them sequentially they work fine, but if i click one and then quickly click a second one (before the first has completed) then the second one will work and the first will fail. This appears to be an issue with update panels and asp.net? Is there anything I can do on that front to enable the above javascript and avoid the setTimeout option?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

橘虞初梦 2024-07-27 09:54:22

它应该有效。 尝试在单击第一个按钮后对第二个按钮进行 setTimeout 调用。 这很丑陋,但也许它会起作用。

function clickButton() {
 document.getElementById('#button1').click();
 setTimeout(button2, 300);
}

function button2() {
 document.getElementById('#button2').click();
}

It should work. Try putting a setTimeout call for the second button after the first button is clicked. It's ugly but maybe it will work.

function clickButton() {
 document.getElementById('#button1').click();
 setTimeout(button2, 300);
}

function button2() {
 document.getElementById('#button2').click();
}
冬天的雪花 2024-07-27 09:54:22

使用 IE 的 fireEvent() 函数怎么样?

function ClickButtons() {
  document.getElementById("Button1").fireEvent("onclick");
  document.getElementById("Button2").fireEvent("onclick");
}

工作示例(在 IE6 中测试):

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <title>Click Test</title>
  </head>

  <body>
    <button id="Button1" onclick="alert('Button1 Clicked');">Click 1</button>
    <button id="Button2" onclick="alert('Button2 Clicked');">Click 2/button>
    <script type="text/javascript">
      document.getElementById("Button1").fireEvent("onclick");
      document.getElementById("Button2").fireEvent("onclick");
    </script>
  </body>
</html>

据我所知,由于您对 click() 的使用是 IE 特定的,所以这个答案也是如此。 事件在不同的浏览器中触发方式不同,但制作包装器并隐藏差异应该不会太难。

How about using IE's fireEvent() function?

function ClickButtons() {
  document.getElementById("Button1").fireEvent("onclick");
  document.getElementById("Button2").fireEvent("onclick");
}

Working example (tested in IE6):

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <title>Click Test</title>
  </head>

  <body>
    <button id="Button1" onclick="alert('Button1 Clicked');">Click 1</button>
    <button id="Button2" onclick="alert('Button2 Clicked');">Click 2/button>
    <script type="text/javascript">
      document.getElementById("Button1").fireEvent("onclick");
      document.getElementById("Button2").fireEvent("onclick");
    </script>
  </body>
</html>

Since your use of click() is to my knowledge IE-specific, so is this answer. Events are fired differently in different browsers, but it shouldn't be too tough to make wrappers and hide the difference.

掩饰不了的爱 2024-07-27 09:54:22

他们都应该触发点击事件。 如果您删除 ajax 请求和其他繁重的工作,而只是在每个事件侦听器中放置一个警报或 console.log,会发生什么。 哦,你如何监听事件?

They should both fire a click event. What happens if you remove your ajax requests and other heavy lifting and just put an alert or console.log in each event listener. Oh, and how are you listening to events?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文