开发者

Why does jQuery .click() not cause postback when used on this <a> tag?

开发者 https://www.devze.com 2023-02-25 05:46 出处:网络
Think this is a quickie for someone. I ha开发者_JAVA百科ve this markup (generated by ASP.Net)...

Think this is a quickie for someone. I ha开发者_JAVA百科ve this markup (generated by ASP.Net)...

<A id=anchorO href="javascript:__doPostBack('anchorO','')">O</A>

This anchor is in an update panel, and if I click it manually a partial postback takes place. However....

$('[ID$="anchor'+initial+'"]').click()   //JavaScript

..selects the correct anchor, but no postback takes place. Why is this?


A click and a href are seen as two different things in Javascript, so you can't do .click() and call the href, regardless if this is calling javascript: or not

Two options:

  1. Just do:

    $('#anchor' + initial).click(function() { __doPostBack('anchorO',''); });
    
  2. Be evil and use eval:

    $('#anchor' + initial).click(function() { eval($(this).attr('href')); });
    


See this question here

It appears that you can't follow the href of an a tag using the click event.

0

精彩评论

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