Use .Net-Repeater with jquery

混江龙づ霸主 提交于 2020-01-11 06:53:05

问题


I have a Repeater (ASP.Net).

<asp:Repeater ID="rep" runat="server">
<ItemTemplate>
<asp:TextBox runat="server" ID="txt"></asp:TextBox>
<asp:CheckBox runat="server" ID="cb" />
</ItemTemplate>

I would like to write Text in the text-box if I click on the checkbox, using jQuery. Is this possible and how.


回答1:


I'd recommend simply using a CSS class and using that to identify the TextBox:

<asp:Repeater ID="rep" runat="server">
  <ItemTemplate>
    <asp:TextBox runat="server" ID="txt"></asp:TextBox>
    <asp:CheckBox runat="server" ID="cb" CssClass="repCheck" />
  </ItemTemplate>

And then:

$('.repCheck').change(function(){
    // via Andre
    $(this).siblings('input[type="text"]').first().val("clicked");
});



回答2:


You'll have to post the html output to get a working code, but it is something like this:

$(function(){
    $('#<%=rep.ClientID  %> input[type="checkbox"]').each(function(){
        $(this).change(function(){
            $(this).siblings('input[type="text"]').first().val("clicked");
        });
    });
});



回答3:


Using jQuery, you can use the ID selector to select the DOM elements with the ID="txt" that you have specified. http://api.jquery.com/id-selector/

You can then update the .text() attribute of those elements. http://api.jquery.com/text/

Something like this should work in the simplest fashion:

$("#txt").text("some new text");



回答4:


Yes, it is. One possibility is the approach by the server:

On the Load Event (in the server) of the CheckBox, you can do the following:

void checkBox_Load(object sender, EventArgs e)
{
     CheckBox chk = (CheckBox) sender;
     TextBox txt = (TextBox) chk.Parent.FindControl("txt");
     chk.Attributes.Add("onclick",
                          string.Format("$(\"#{0}\").val('{1}');", txt.ClientID, "newValue");
}

EDIT: It was missing the # in the jQuery selector.



来源:https://stackoverflow.com/questions/6735383/use-net-repeater-with-jquery

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