如何用jQuery获取href属性
我如何才能获取已被母版页修改的 asp:LinkButton 的 ID,以便我可以使用 javascript/jQuery 模拟单击?
通常,通过这个,我会获得“ctl05_butSaveAssociation”作为“_id”的值:
function ClickSaveButton() {
var _id = $('a[id$="butSaveAssociation"]').attr("ID");
__doPostBack(_id, '');
return false;
}
这不起作用,因为 ID 由于某种原因再次更改,正如我们可以看到使用 FireBug 并将鼠标悬停在 asp:LinkButton 上:
<a style="border-style: none; display: inline-block;" href="javascript:__doPostBack('ctl05$butSaveAssociation','')" class="CommandButton" id="ctl05_butSaveAssociation" onclick="SaveAssociation();" tabindex="0">Save</a>
所以事实上,如果我硬编码正确的 ID,它会起作用:
function ClickSaveButton() {
var _id = "ctl05$butSaveAssociation"; // hardcoded
__doPostBack(_id, '');
return false;
}
我如何使用 jQuery 动态实现这个?
How would I be able to obtain the ID of a asp:LinkButton that has been modified by the master page so that I can simulate a click using javascript/jQuery ?
Normally, with this, I would obtain 'ctl05_butSaveAssociation' as the value for '_id':
function ClickSaveButton() {
var _id = $('a[id$="butSaveAssociation"]').attr("ID");
__doPostBack(_id, '');
return false;
}
This doesn't work, because the ID changes again for some reason, as we can see using FireBug and hovering over the asp:LinkButton we have:
<a style="border-style: none; display: inline-block;" href="javascript:__doPostBack('ctl05$butSaveAssociation','')" class="CommandButton" id="ctl05_butSaveAssociation" onclick="SaveAssociation();" tabindex="0">Save</a>
So in fact, if I hardcode the right ID, it works with:
function ClickSaveButton() {
var _id = "ctl05$butSaveAssociation"; // hardcoded
__doPostBack(_id, '');
return false;
}
How could I implement this dynamically using jQuery?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
虽然 HTML 生成的 ID 具有
_
(因为是基于ClientID
属性),ASP.NET 帮助程序方法需要$
。我真的不知道为什么它不能在所有情况下使用下划线,但就是这样......一件简单的事情就是使用像
_id.replace("_", "$")< 这样的替换/代码>。完整代码:
但我在这里想...为什么你不调用点击事件?
While the HTML generated ids have
_
(because are genereted based on theClientID
property), the ASP.NET helper methods expects$
. I really don't know exactly why it can't use the underscore on all cases, but this is it...One easy thing is just using replace like
_id.replace("_", "$")
. Complete code:But I'm here thinking... Why you just isn't calling the click event?
一种方法是在其周围放置一个
。像这样的东西:
然后在你的 jQuery 中,
即使它的 id 发生变化,它也会给你一种识别按钮的方法。
One way would be to place a
<span>
around it. Something like this:And then in you jQuery
That would give you a way to identify the button even if it's id changes.
如果您使用的是 ASP.NET 4 或更高版本,则可以使用
ClientIDMode
使 ID 与您键入的 ID 保持一致:http://beyondrelational.com/blogs/hima/ archive/2010/07/16/all-about-client-id-mode-in-asp-net-4.aspx
http://www.west-wind.com/weblog/posts/2009/Nov/07/ClientIDMode-in-ASPNET -40
If you are in ASP.NET 4 or better, you can use
ClientIDMode
to make the IDs stay the same as you typed them:http://beyondrelational.com/blogs/hima/archive/2010/07/16/all-about-client-id-mode-in-asp-net-4.aspx
http://www.west-wind.com/weblog/posts/2009/Nov/07/ClientIDMode-in-ASPNET-40