How to get asp:button ID in jQuery

依然范特西╮ 提交于 2020-01-16 03:40:07

问题


I'm making .aspx file and using jQuery to do key press function. I made few <asp:Imagebutton> so write specific button id in jQuery part.

$(document).keypress(function (e) {
                if (e.which === 13) {
                    $("#ImageSave").click();
                }
            });

I wrote $("#ImageSave") following aspx button id. but asp:button id in html is different with my original button id. so I change jQuery code id part. $("#ImageSave").click(); to $("#MainContent_ImageSave"). But its click event is not fired.

asp.net

<asp:ImageButton ID="ImageSave" runat="server" imageurl="img/button_save.jpg"  
       AutoPostBack="True" onclick="ImageSave_Click" />    

html

<input name="ct100$MainContent$ImageSave" id="MainContent_ImageSave" 
     type="image" src="img/button_save.jpg" autopostback="True"></input>   

I think this problem because of using asp id in jQuery way is wrong. Would be nice if you can help me with this or atleast point me to the right direction :)


回答1:


You can use this as id

<%= ImageSave.ClientID %>

so instead of this

 $("#ImageSave").click();

use

 $("#<%= ImageSave.ClientID %>").click();

Or you can use ClientIDMode="Static" so that the id doesn't change at runtime. If you are using asp.net 4.0 and above.

<asp:ImageButton ID="ImageSave" runat="server" imageurl="img/button_save.jpg"  
   AutoPostBack="True" onclick="ImageSave_Click" ClientIDMode="Static" />    


来源:https://stackoverflow.com/questions/28270970/how-to-get-aspbutton-id-in-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!