如何在Repeater中找到Literal和Link按钮

时间:2014-01-22 06:45:02

标签: javascript jquery c#-4.0

<asp:Repeater runat="server" ID="rpt">
   <ItemTemplate>                    
        <asp:LinkButton ID="MyLink" runat="server" Text="Fair" ></asp:LinkButton>
          <span class="literal">
            (<asp:Literal runat="server" ID="Lit1" ClientIDMode="Static" Text='<%# Eval("Value") %>'></asp:Literal>)
          </span>
   </ItemTemplate>

如上所示,我在Repeater中有一个文字和Link按钮。我想在链接按钮上获取Literal的值。

我已使用给定代码查找链接按钮单击:

$('#MyLink').click(function () {    
    alert($(this).attr('MyLink'));  
});

由于Link按钮位于Repeater内部,因此失败。任何人都可以根据我的要求更新代码。感谢。

2 个答案:

答案 0 :(得分:0)

服务器控件在javascript中使用不同的Id。尝试:

$(document).ready(function(){
  $('#<%= MyLink.ClientID %>').click(function () {    
     alert($(this).attr('MyLink'));  
  });
});

答案 1 :(得分:0)

向您的class添加link

<asp:LinkButton ID="MyLink" class="MyLink" runat="server" Text="Fair" ></asp:LinkButton>

<强> Jquery的

$(function(){// write your code in document ready
     $('.MyLink').click(function () {    
          alert($(this).attr('Text'));
     });
});

已更新,如果您想获取literal text,请在class literal添加literal element

(<asp:Literal runat="server" ID="Lit1" ClientIDMode="Static" class='literal' Text='<%# Eval("Value") %>'></asp:Literal>)

并改变jquery code之类的,

$(function(){// write your code in document ready
     $('.MyLink').click(function () {    
          alert($(this).next('.literal').attr('Text'));
     });
});

在查看HTML

后尝试
$(function(){
    $('.clsMyLink') .click(function(e){
       e.preventDefault();
        alert($(this).next('.literal').text());
    });
});

Demo