是否可以在javascript中进行两个.click方法调用 [英] Is it possible to make two .click method calls in javascript

查看:67
本文介绍了是否可以在javascript中进行两个.click方法调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下javascript代码:

I have the following javascript code:

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

似乎仅单击了Button2.如果我颠倒了语句的顺序,那么似乎只有Button1(然后称为2nd)起作用.

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.

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

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)

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

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?

推荐答案

如何使用IE的fireEvent()函数?

How about using IE's fireEvent() function?

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

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

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>

由于据我所知,您对 click()的使用是特定于IE的,因此此答案也是如此.在不同的浏览器中触发事件的方式有所不同,但是包装包装并隐藏差异并不难.

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.

这篇关于是否可以在javascript中进行两个.click方法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆