简体   繁体   English

从jquery获取data-id

[英]Getting data-id from jquery

I'm using ASP.net/C# with jQuery and i'm trying to get the value in data-id. 我正在使用ASP.net/C#和jQuery,我正在尝试获取data-id中的值。 I have the following code: 我有以下代码:

Markup: 标记:

 <asp:Button runat="server" ID="btnAddLocations" Text="Update" CssClass="btn btn-info pull-right addLocation" />

Code behind (basically adds the data-id attribute) 代码隐藏(基本上添加了data-id属性)

SPID = "3"
btnAddLocations.Attributes.Add("data-id", SPID);

jQuery: jQuery的:

$(".addLocation").click(function () {
        var id = ($(".addLocation").data("spid"));
        alert(id);
        var url = "popups/AddLocations.aspx?spid=" + id;
        window.open(url, "Add Locations", "width=600,height=550");
        return false;
    });

However, ID always comes back as undefined 但是,ID总是以未定义的形式返回

I think you should use 我想你应该用

var id = $(this).data("id");

OR 要么

var id = $(this).attr("data-id");

I think you try to get value set as 'data-spid' in HTML DOM 我认为你试图在HTML DOM中将值设置为'data-spid'

SPID = "3"
btnAddLocations.Attributes.Add("data-spid", SPID);

there is more easy way to pass id from button to javascript 有更简单的方法将id从按钮传递到javascript

   function DoEdit(e)
   {
       alert(e.toString());
   };

< input id="btnAddLocations" onclick="DoEdit('<%# Eval("SPID") %>')" value="GetID"/> <input id =“btnAddLocations”onclick =“DoEdit('<%#Eval(”SPID“)%>')”value =“GetID”/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM