Why do I get an error with .Click() Event for an Asp:LinkButton in Firefox?
I am attempting to fire the click() event when a button is clicked.
This currently works for me in IE however I receive the following error in firefox:
"link.click is not a function"
I have been trawling google regarding this and found out the .click event is not supported in all versions of firefox.
Is anyone able to provide an alternative? Code below:
<asp:LinkButton ID="ButtonNext" runat="server" CssClass="RemoveLinkStyle" TabIndex="1"></asp:LinkButton>
<table id="tblB开发者_开发问答uttonNext" runat="server" cellpadding="0" cellspacing="0" class="pwbtn" onclick="ButtonClick(ButtonNext);" TabIndex="1" onmouseout="this.className='pwbtn';" onmouseover="this.className='pwbtnh';">
<tr>
<td class="a1"></td>
<td class="a2">
<%= this.resourceManager.GetString("nextstep") %>
</td>
<td class="a3"></td>
<td class="spacer"></td>
</tr>
</table>
function ExecuteLink(linkID)
{
var link = document.getElementById(linkID);
link.click();
}
function ButtonClick(linkID)
{
PreNavigationScript();
CallShowBlocker();
ExecuteLink(linkID);
}
I'd use jQuery for this. The click
event exposed by jQuery is designed to be cross browser compatible and you'll likely find it works more consistently than what you're currently using.
Your method will then become:
function ExecuteLink(linkID)
{
$("#" + linkID).click();
}
(You probably want to check whether $(linkID) returns an element also)
Setting CausesValidation="False"
will solve the problem
If you are not using jQuery then your alternative is pretty hacky mouse event for firefox, e.g.
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false,
false, false, false, 0, null);
AnchorFieldObj.dispatchEvent(evt);
as per mozilla.dev.tech.dom
However, another alternative is to call the function handling your button click event. View Source and check what it is, it'll be something like __doPostback('ButtonNext',...);
and call it in your ExecuteLink() funct.
I tried the click()-Method of Jquery too, but the event was never triggered.
You can access the generated javascript Method of the Linkbutton directly:
__doPostBack('ctl00$MainContentPlaceholder$MyLinkButtonId','');
The ASP-Linkbutton is rendered to a input field and an anchor in the final HTML-Code. The first parameter is the (client-)name of this inputfield.
Just add this code snippet in your .master page or any other appropiate page just before the </body>
tag:
<script language="javascript" type="text/javascript">
<!--
function __doPostBack(eventTarget, eventArgument) {
var theform;
if (window.navigator.appName.toLowerCase().indexOf("microsoft") > -1) {
theform = document.aspnetForm;
}
else {
theform = document.forms["aspnetForm"];
}
theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
theform.__EVENTARGUMENT.value = eventArgument;
theform.submit();
}
// -->
</script>
Replace 'aspnetForm' with your own.
精彩评论