【问题标题】:Get label of element in jquery获取jquery中元素的标签
【发布时间】:2013-10-12 22:52:28
【问题描述】:

我想将元素的标签作为字符串并在 jquery 中提醒该字符串。我在下面写了代码,但我得到了 [object][Object] 值。我也尝试了 html() 和 text() 但它没有用。

 function seatObject(id, label, status, token){
        this.id = id;
        this.label = label; 
        this.status = status;
        this.token = token;
    }

    for (var x = 0; x < 5; x++) {            
      var temp = new seatObject("#" + x, "label" + x, "available", "");
      seats[x] = temp;                        
      $("#" + x).click(function () {
        currentSeat = $(this).attr("id");
        var label = $("label[for="+$(this).attr('id')+"]");
        alert(label);   
      });
    } 

这也是我的标签;

<rect style="fill: #888888;" id="0" width="35.823246" height="35.823246" x="299.87155" y="65.999405" class="seatObj" label="A24"></rect>

【问题讨论】:

  • var label = $("label");警报(标签。文本());这对我有用fiddle.jshell.net/kU7Pb
  • 我试过了,但我得到了 [object][Object] 值
  • 你的seatObject函数是做什么的?可以发一下代码吗?
  • 函数座位对象(id,标签,状​​态,令牌){ this.id = id; this.label = 标签; this.status = 状态; this.token = 令牌; }

标签: jquery asp.net-mvc asp.net-mvc-4


【解决方案1】:

如果您使用的是 asp 标签,请使用此标签

var labelText  = $('#<%= label.ClientID %>').val();

或者如果您使用的是 html 标签,请使用此

var labelText = $('#label').val();

这应该对你有用..

【讨论】:

    【解决方案2】:

    因为您使用的是服务器端对象,所以为您的标签指定类。

    <label runat="server" id="myLabel" CssClass="myLabelClass"></label>
    
    alert($(".myLabelClass").text());
    

    应该可以。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-17
      • 2011-01-26
      • 2012-05-19
      • 1970-01-01
      • 2019-07-19
      • 2013-05-13
      • 1970-01-01
      相关资源
      最近更新 更多