开发者

onclick event in anchor tag not working in IE

The following works fine in Firefox and Chrome, but IE 8 will not call the submit() method when the anchor link is clicked.

开发者_C百科
<a href="javascript:void(0);" onclick="submit();">Sign In</a>

The submit method is defined on the same page as follows:

<head>
<script type="text/javascript">

function submit()
{
// other code                                              
document.forms[0].submit();
}  

</script>
</head>


Can you provide a bit more context? Such as where and how the submit function is defined? With just the above, it should work -- except:

You probably also want to return false; in there though, to cancel the default action. E.g.:

<a href="javascript:void(0);" onclick="submit();return false;">Sign In</a>

It may be that the default action is happening immediately after the submit and interfering with it.

Edit: Just for fits and giggles, try using a different name for your submit function. IE has namespacing issues. If you have anything with the id or name attribute set to "submit", for instance, that could be an issue...


For others who have come across this problem more recently, for me it was due to IE 11 using compatibility mode on the site. It saw I was on the same domain, assumed the site was an intranet site and automatically switched to compatibility mode which broke some of the Javascript (and caused some other unexpected display issues).

You can turn off this 'feature' by clicking on the settings cog and choosing "Compatibility View Settings". There is a tick box that was set called "Display intranet sites in Compatibility View".


Try using

<a onclick="submit();">Sign In</a>

or

<a href="javascript:submit();">Sign In</a>


If you need to input the value to the javascript function, make sure that uses the single quotation instead of the double quotation.

For example:

<a href="#" onclick="your_function('input');">Sign In</a>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜