[英]How to get value of selected label from repeater using javascript
我已經綁定了轉發器,並且想在選中特定復選框時獲取標簽的值:
$(function() { $('.toggleCheck').change(function() { console.log('Toggle: ' + $(this).prop('checked')) RepeaterData(); }) }) function RepeaterData() { var size = $('.nameLabel').length; console.log('size' + size); for (i = 0; i < size; i++) { var name = $('.nameLabel').eq(i).text(); var id = $('.IdLabel').eq(i).text(); //var result = document.getElementById('result'); alert(name); // result.innerText += name + " \\t " + id + "\\n"; } }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <asp:Repeater ID="rptCust" runat="server"> <HeaderTemplate> <div class="table-responsive"> <table class="table table-striped table-bordered table-hover table-highlight" data-provide="datatable" data-display-rows="10" data-info="true" data-search="true" data-length-change="true" data-paginate="true"> <thead> <tr> <th data-filterable="true">No</th> <th>name</th> <th>Action</th> </tr> </thead> <tbody> </HeaderTemplate> <ItemTemplate> <tr> <td> <asp:Label ID="lblId" CssClass="IdLabel" runat="server" Text='<%#Eval("id")%>' Style="display: none"></asp:Label> <asp:Label ID="lblno" CssClass="nameLabel" runat="server" Text='<%#Eval("no")%>'></asp:Label> </td> <td> <asp:Label ID="lblname" runat="server" Text='<%#Eval("name")%>'></asp:Label> </td> <td> <input id="Checkbox1" commandname="Edit" data-toggle="toggle" type="checkbox" checked='<%# (Eval("status")).ToString() == "1" ? true : false %>' runat="server" class="toggleCheck"> </td> </tr> </ItemTemplate> <FooterTemplate> </tbody> </table> </div> </FooterTemplate> </asp:Repeater>
我已經寫了上面的代碼。
現在,當我單擊復選框時,我需要使用javascript獲取該復選框的ID。
我試圖獲取該值,但它為我提供了所有值,而不是選中/未選中復選框上的特定ID。
當我選中/取消選中復選框時,如何獲取特定ID的值?
你可以這樣做...
$('.toggleCheck').click(function(){
console.log($(this).attr('id'));
})
或者如果您想確定是否感到沮喪...
$('.toggleCheck').click(function(){
if($(this).is(':checked')){
console.log($(this).attr('id'));
}
})
我已經解決了。 下面是代碼。
我添加了var id = $(this).closest('tr').find('td:first-child').text();
與上面的行,我能夠獲得選中/未選中復選框上的特定ID
$(function() {
$('.toggleCheck').change(function() {
console.log('Toggle: ' + $(this).prop('checked'))
var id = $(this).closest('tr').find('td:first-child').text();
})
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.