jQuery:触发 <asp:LinkBut​​ton> 点击

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10172284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 08:54:06  来源:igfitidea点击:

jQuery: trigger <asp:LinkButton> click

jqueryasp.netclick

提问by Keith L.

i need to trigger the click of an <asp:LinkButton>.

我需要触发一个<asp:LinkButton>.

Example1

示例 1

The following thing is working:

以下事情正在起作用:

<asp:Button ID="myBtn" runat="server" />

$("#myBtn").trigger("click");

Example2

例2

Now the same thing with the LinkButton is notworking:

现在用的LinkBut​​ton同样的事情工作:

<asp:LinkButton ID="myBtn" runat="server" />

$("#myBtn").trigger("click");

I need to trigger the click event of an asp:LinkButton.

我需要触发 asp:LinkBut​​ton 的点击事件

采纳答案by Curt

Add a CssClassproperty to your LinkButtonand use that as your selector.

CssClass向您添加一个属性LinkButton并将其用作您的选择器。

Other answers have suggested using ASP.NET ClientIDto get the rendered ID of the LinkButton, which is fine, but this means you have to have your script inline in the page, and the script will only work for this 1 button.

其他答案建议使用 ASP.NETClientID来获取LinkButton.

Using a class means you can have multiple elements triggering the same event, and it means you don't need to use inline server-side code to get it to work.

使用类意味着您可以让多个元素触发同一个事件,这意味着您不需要使用内联服务器端代码来使其工作。

<asp:LinkButton ID="myBtn" runat="server" CssClass="myLink" />

$(".myLink").trigger("click");

回答by Gloopy

Instead of

代替

$("#myBtn").trigger("click");

Try this

尝试这个

eval($("#myBtn").attr('href'));

See this answer for more info: https://stackoverflow.com/a/943441/1207991

有关更多信息,请参阅此答案:https: //stackoverflow.com/a/943441/1207991

回答by Govind Malviya

Use this line

使用这条线

$("#<%= myBtn.ClientID %>").trigger("click");

回答by coder

You're giving same IDto both of them which don't work.Try to change it.

你给ID他们两个都不起作用。尝试改变它。

If you're using master page then use as shown below:

如果您使用的是母版页,则使用如下所示:

  <script type="text/javascript">
          $(document).ready(function () {
              $("#<%=myBtn.ClientID%>").click(function () {
              $('#<%=hyperlinkID.ClientID%>').trigger('click');
          });
  </script>

If you're don't use master page then use the code below:

如果您不使用母版页,请使用以下代码:

<script type="text/javascript">
         $(document).ready(function () {
            $("#myBtn").click(function () {
             $('#hyperlinkID').trigger('click');
         });
 </script>

回答by Dimitar Yankovski

Never use eval() because of security leaks and vulnerability. Best way to do this is with using of native js click event like that.

由于安全漏洞和漏洞,切勿使用 eval()。最好的方法是使用像这样的原生 js 单击事件。

$('[selector]')[0].click();

in your case

在你的情况下

$("#myLink")[0].click();