retrieve ID of server control using jQuery

2019-01-19 17:00发布

How do I get the ID of a server control with jQuery?

E.g. I have

<asp:Label ID="label1" runat="server""></asp:Label>

and now I want to get "label1",

var id = ??

6条回答
兄弟一词,经得起流年.
2楼-- · 2019-01-19 17:08

jQuery runs on the client side so would only be able to access the ID of the html element rather than the asp control on the server.

查看更多
相关推荐>>
3楼-- · 2019-01-19 17:16
var labelID = $('#<%= label1.ClientID %>');

You need to get the client ID.

If you just need the ID, and not the actual value of the control, then you don't even need jQuery.

var labelID  = '<%= label1.ClientID %>';
查看更多
神经病院院长
4楼-- · 2019-01-19 17:22
var $lblObj = $("label[id$='label1']:first")
查看更多
地球回转人心会变
5楼-- · 2019-01-19 17:23

If you use ASP.NET 4.0 you can set attribute ClientIDMode="Static" and your code will looks following way:

<asp:Label ID="label1" runat="server" ClientIDMode="Static"></asp:Label>

js:

var id = 'label1';
查看更多
我命由我不由天
6楼-- · 2019-01-19 17:23

Labels render as span tags. So if you want to select all the Labels:

    $(document).ready(function()
    {
        $Labels = $("span");

        $Labels.each(function()
        {
            alert(this.id); 
        });
    });
查看更多
狗以群分
7楼-- · 2019-01-19 17:25

Are you using master page. If yes give ContentPlaceHolderID along with control id.

Eg:

 jQuery("#ContentPlaceHolderID_ControlId").val;
   jQuery("#body_label1").text;

You can see this in Viewsource

查看更多
登录 后发表回答