admin管理员组

文章数量:1022631

How do I get the value of a hidden field from a repeater into a javascript function? In essence, with the following repeater:

<asp:Repeater runat="server" ID="dtrRedStations">
<ItemTemplate>
    <div class="RepeaterClass">
        <asp:HiddenField runat="server" ID="hdnStationRedID" />
        <asp:HiddenField runat="server" ID="hdnStationRedEXID" />
        <div class="col-xs-12">Name: <asp:Label runat="server" ID="lblStationRedName" Text='<%#Bind("Name")%>'></asp:Label></div>
        <div class="col-xs-12">Number: <asp:Label runat="server" ID="lblStationRedNumber" Text='<%#Bind("Number")%>'></asp:Label></div>
        <div class="col-xs-12">Job:<asp:Label runat="server" ID="lblStationRedJobClass" Text='<%#Bind("Job")%>'></asp:Label></div>
    </div>
</ItemTemplate>

I want to add an clickable event to the div that takes a value from the first hiddenfield value and uses it to set the second hiddenfield value, in javascript. But I can't work out how to tell at runtime which instance of the repeaters bound values I am in.

How do I get the value of a hidden field from a repeater into a javascript function? In essence, with the following repeater:

<asp:Repeater runat="server" ID="dtrRedStations">
<ItemTemplate>
    <div class="RepeaterClass">
        <asp:HiddenField runat="server" ID="hdnStationRedID" />
        <asp:HiddenField runat="server" ID="hdnStationRedEXID" />
        <div class="col-xs-12">Name: <asp:Label runat="server" ID="lblStationRedName" Text='<%#Bind("Name")%>'></asp:Label></div>
        <div class="col-xs-12">Number: <asp:Label runat="server" ID="lblStationRedNumber" Text='<%#Bind("Number")%>'></asp:Label></div>
        <div class="col-xs-12">Job:<asp:Label runat="server" ID="lblStationRedJobClass" Text='<%#Bind("Job")%>'></asp:Label></div>
    </div>
</ItemTemplate>

I want to add an clickable event to the div that takes a value from the first hiddenfield value and uses it to set the second hiddenfield value, in javascript. But I can't work out how to tell at runtime which instance of the repeaters bound values I am in.

Share Improve this question edited Sep 8, 2017 at 15:22 Nisarg Shah 14.6k6 gold badges38 silver badges57 bronze badges asked Sep 8, 2017 at 15:09 DavidDavid 2351 gold badge6 silver badges19 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

Use below javascript function to get value from first hidden field from row and assign value to next hidden field

Used Jquery to find child elements here

function pageLoad() {
    $(".RepeaterClass").click(function () {
        var div = $(this);
        var firstHiddenFieldValue = div.find('[id*=hdnStationRedID]').val();
        //alert(firstHiddenFieldValue);
        var secondHiddenField = div.find('[id*=hdnStationRedEXID]');
        secondHiddenField.val(firstHiddenFieldValue);
        //alert(secondHiddenField.val());
    });
}

How do I get the value of a hidden field from a repeater into a javascript function? In essence, with the following repeater:

<asp:Repeater runat="server" ID="dtrRedStations">
<ItemTemplate>
    <div class="RepeaterClass">
        <asp:HiddenField runat="server" ID="hdnStationRedID" />
        <asp:HiddenField runat="server" ID="hdnStationRedEXID" />
        <div class="col-xs-12">Name: <asp:Label runat="server" ID="lblStationRedName" Text='<%#Bind("Name")%>'></asp:Label></div>
        <div class="col-xs-12">Number: <asp:Label runat="server" ID="lblStationRedNumber" Text='<%#Bind("Number")%>'></asp:Label></div>
        <div class="col-xs-12">Job:<asp:Label runat="server" ID="lblStationRedJobClass" Text='<%#Bind("Job")%>'></asp:Label></div>
    </div>
</ItemTemplate>

I want to add an clickable event to the div that takes a value from the first hiddenfield value and uses it to set the second hiddenfield value, in javascript. But I can't work out how to tell at runtime which instance of the repeaters bound values I am in.

How do I get the value of a hidden field from a repeater into a javascript function? In essence, with the following repeater:

<asp:Repeater runat="server" ID="dtrRedStations">
<ItemTemplate>
    <div class="RepeaterClass">
        <asp:HiddenField runat="server" ID="hdnStationRedID" />
        <asp:HiddenField runat="server" ID="hdnStationRedEXID" />
        <div class="col-xs-12">Name: <asp:Label runat="server" ID="lblStationRedName" Text='<%#Bind("Name")%>'></asp:Label></div>
        <div class="col-xs-12">Number: <asp:Label runat="server" ID="lblStationRedNumber" Text='<%#Bind("Number")%>'></asp:Label></div>
        <div class="col-xs-12">Job:<asp:Label runat="server" ID="lblStationRedJobClass" Text='<%#Bind("Job")%>'></asp:Label></div>
    </div>
</ItemTemplate>

I want to add an clickable event to the div that takes a value from the first hiddenfield value and uses it to set the second hiddenfield value, in javascript. But I can't work out how to tell at runtime which instance of the repeaters bound values I am in.

Share Improve this question edited Sep 8, 2017 at 15:22 Nisarg Shah 14.6k6 gold badges38 silver badges57 bronze badges asked Sep 8, 2017 at 15:09 DavidDavid 2351 gold badge6 silver badges19 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

Use below javascript function to get value from first hidden field from row and assign value to next hidden field

Used Jquery to find child elements here

function pageLoad() {
    $(".RepeaterClass").click(function () {
        var div = $(this);
        var firstHiddenFieldValue = div.find('[id*=hdnStationRedID]').val();
        //alert(firstHiddenFieldValue);
        var secondHiddenField = div.find('[id*=hdnStationRedEXID]');
        secondHiddenField.val(firstHiddenFieldValue);
        //alert(secondHiddenField.val());
    });
}

本文标签: jqueryHow do I get a value from a repeater in javascriptStack Overflow