简体   繁体   English

使用jquery查找repeater复选框选中的值

[英]use jquery find repeater checkbox checked value

I have a checkbox template field within a repeater in c# 我在c#中的转发器中有一个复选框模板字段

bind the repeater using asp.net c# 使用asp.net c#绑定转发器

<asp:Repeater ID="rpt_detail" runat="server">
    <ItemTemplate>
        <table>
            <tr>
                <td style="width: 10%; padding-top: 10px;">
                    <input type="checkbox" id="selectit" name="selectit" value='<%# Eval("mdID") %>' />
                </td>
                <td>
                    <asp:Label ID="mdID" runat="server" Visible="false" Text='<%# Eval("mdID") %>'></asp:Label>
                </td>
                <td>
                    <asp:Label ID="lbl_xprice" runat="server" Text='<%# Eval("mdPrice") %>'></asp:Label>
                </td>
            </tr>
        </table>
    </ItemTemplate>
</asp:Repeater>

and javascipt 和javascipt

<script>
    function aaaaa() {
        var selectit = $('input[name="selectit"]').val();

        $.ajax({
            url: "ajaxservice/getinchar.aspx",
            type: 'POST',
            data: { selectw: selectit },
            success: function (result) {
                $("#testview").html(result);
            }
        });
    }
</script>

How can I get checkbox checked value ? 如何获取复选框选中的值?

You can loop all the checkboxes in the repeater with jQuery. 您可以使用jQuery循环转发器中的所有复选框。

<script type="text/javascript">
    function getCheckBoxValues() {
        $('#myRepeater input[type="checkbox"]').each(function () {
            if ($(this).prop('checked') == true) {
                alert($(this).val());
            }
        });
    }
</script>

But in order for this to work you have to wrap the Repeater with a <div> with a unique ID. 但为了实现这一点,您必须使用具有唯一ID的<div>包装Repeater。

<div id="myRepeater">
    <asp:Repeater ID="rpt_detail" runat="server"></asp:Repeater>
</div>

Not really related to the solution, but you are getting duplicate ID's ( selectit ) in your checkboxes inside the Repeater. 与解决方案无关,但您在Repeater内的复选框中获得了重复的ID( selectit )。 Better use the asp.net CheckBox Control. 更好地使用asp.net CheckBox控件。

And you can better set the <table> </table> outside the Repeater. 您可以更好地在Repeater外部设置<table> </table>

<table id="myRepeater">
    <asp:Repeater ID="rpt_detail" runat="server">
        <ItemTemplate>
            <tr>
                <td> content... </td>
            </tr>
        </ItemTemplate>
    </asp:Repeater>
</table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM