2009-06-03 69 views
14

为什么不能正常工作?LinkBut​​ton不会在点击()上调用

<script src="Scripts/jquery-1.3.2.js" type="text/javascript"></script> 
    <script type="text/javascript"> 
     $(document).ready(function() { 
      $('.myButton').click(); 
     }); 
    </script> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <asp:LinkButton id="ttt" runat="server" PostBackUrl="~/Default.aspx" CssClass="myButton">Click</asp:LinkButton> 
    </div> 
    </form> 

回答

35

你想提交表单,或添加Click事件用这个? 你的链接按钮转换为

<a id="ttt" class="myButton" href="javascript:WebForm_DoPos[...]">Click</a> 

,所以它没有在点击的JavaScript。因此,.click();什么都不做。
我没有测试它,但也许这将工作:

eval($('.myButton').attr('href')); 
1

您需要的时候点击事件引发

$(document).ready(function() { 
     $('.myButton', '#form1') 
      .click(function() { 
       /* 
        Your code to run when Click event is raised. 
        In this case, something like window.location = "http://..."      
        This can be an anonymous or named function 
       */ 
       return false; // This is required as you have set a PostbackUrl 
           // on the LinkButton which will post the form 
           // to the specified URL 
      }); 
    }); 

我已经测试以上ASP.NET 3.5和它的作品如预期分配事件处理程序来火。

Linkbutton上还有OnClientClick属性,它指定客户端脚本在引发click事件时运行。

我可以问你想达到什么吗?

+0

谢谢,但没有尝试that..that不起作用 – Hitz 2009-06-03 06:53:26

+0

它的工作,你需要执行的功能有所行动。你是通过AJAX添加linkbutton还是在Page加载之后? – 2009-06-03 07:02:24

+0

是否正确引用了jQuery框架?你可以通过尝试$(document).ready(function(){alert(“Hello”);}); – 2009-06-03 07:09:03

0

单击事件处理程序必须实际执行一个操作。试试这个:

$(function() { 
    $('.myButton').click(function() { alert('Hello!'); }); 
}); 
0

你需要给LinkBut​​ton的一个的CssClass =“myButton的”,然后在上面

$(document).ready(function() { 
     $('.myButton').click(function(){ 
      alert("hello thar"); 
     }); 
    }); 
0

这是一个艰难的一个。据我了解,你想模仿在JavaScript代码中点击按钮的行为。问题是ASP.NET向onclick处理程序添加了一些奇特的JavaScript代码。

在jQuery中手动触发事件时,只会执行由jQuery添加的事件代码,而不会执行onclick属性或href属性中的JavaScript。所以这个想法是创建一个新的事件处理程序,它将执行在属性中定义的原始JavaScript。

什么我会建议尚未经过测试,但我给它一个镜头:

$(document).ready(function() { 

// redefine the event 
$(".myButton").click(function() { 
    var href = $(this).attr("href"); 

    if (href.substr(0,10) == "javascript:") { 
     new Function(href.substr(10)).call(this); 
     // this will make sure that "this" is 
     // correctly set when evaluating the javascript 
     // code 
    } else { 
     window.location = href; 
    } 

    return false; 
}); 

// this will fire the click: 

$(".myButton").click(); 

}); 
0

只是为了澄清,只有火狐从这个问题受到影响。见http://www.devtoolshed.com/content/fix-firefox-click-event-issue。在FireFox中,anchor(a)标签没有click()函数以允许JavaScript代码直接模拟点击事件。他们允许您映射锚点标记的点击事件,只是不要用click()函数来模拟它。

幸运的是,ASP.NET将JavaScript回发代码放入href属性中,您可以在其中获取它并在其上运行eval。 (或者只是调用window.location.href = document.GetElementById('LinkBut​​ton1')。href;)。

或者,您可以调用__doPostBack('LinkBut​​ton1');注意'LinkBut​​ton1'应该被LinkBut​​ton的ClientID/UniqueID取代以处理命名容器,例如, UserControls,MasterPages等。

乔丹丽格

3

如果需要的LinkBut​​ton的OnClick服务器端事件,火灾,你需要使用__doPostBack(的eventTarget,eventArgument)。

例如:

<asp:LinkButton ID="btnMyButton" runat="Server" OnClick="Button_Click" /> 

<script type="text/javascript"> 

function onMyClientClick(){ 
    //do some client side stuff 

    //'click' the link button, form will post, Button_Click will fire on back-end 
    //that's two underscores 
    __doPostBack('<%=btnMyButton.UniqueID%>', ''); //the second parameter is required and superfluous, just use blank 
} 

</script> 
10

触发( '点击')触发jQuery的单击事件监听器.NET是不是迷上了。你可以只触发JavaScript的点击事件,这将去(或者在这种情况下运行)什么是在href属性:

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

($('.myButton').length ? $('.myButton') : $('<a/>'))[0].click(); 

如果你不知道该按钮是怎么回事出现在页面上。

相关问题