简体   繁体   English

如何使用JQUERY获取没有id或class的复选框onchange的值?

[英]How to get value of checkbox onchange without id or class using JQUERY?

I wants to alert checkbox value if selected.And it will not have any id or class . 如果要选中复选框,我想提醒复选框值。它将没有任何id或class It's a listbox item which is having checkbox. 这是一个具有复选框的列表框项目。 User may select multiple checkbox. 用户可以选择多个复选框。

Here is my ASPX code 这是我的ASPX代码

<asp:ListBox ID="ListStoreID" CssClass="multiselect" runat="server" SelectionMode="Multiple" DataSourceID="SqlDataSource3" DataTextField="ID" DataValueField="ID">

</asp:ListBox>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:datareload33 %>" SelectCommand="SELECT [ID] FROM [Store] WHERE ([Inactive] = @Inactive)">
     <SelectParameters>
          <asp:Parameter DefaultValue="false" Name="Inactive" Type="Boolean" />
     </SelectParameters>
</asp:SqlDataSource>

<script type="text/javascript">
    $(function () {
        $('[id*=ContentPlaceHolder1_ListStoreID]').multiselect({
            includeSelectAllOption: false,
            buttonText: function (options, select) {
                return 'StoreID';
            },
            buttonTitle: function (options, select) {
                var labels = [];
                options.each(function () {
                    labels.push($(this).text());
                });
                return labels.join(' - ');
            }
        });
    });
    </script>

I am using SQLDataSource to bring the list from database. 我正在使用SQLDataSource从数据库中获取列表。 So It will not have any id or class. 因此,它将没有任何ID或类。

screanshot screanshot

aspx代码的屏幕截图

客户端代码

Update 更新

最后截图

In jquery you can bind events to either element types or a collection of element child. 在jquery中,您可以将事件绑定到元素类型或元素子元素的集合。 I would try the following 我会尝试以下

$('input[type="checkbox"]').change(function() {
    if(this.checked){
            //alert whatever you need.. probably this.value
            alert(this.value); 
    }
});

You can select a group of items just by its node type. 您可以仅通过其节点类型来选择一组项目。 Take a look at the next snippet, the selector selects all the inputs inside the element with the class multiselect-container . 看一下下一个代码片段,选择器使用multiselect-container类选择元素内的所有输入。 Then you can add a "change" event to that selection. 然后,您可以向该选择添加“更改”事件。

 var checkboxes = $(".multiselect-container input"); checkboxes.on("change", function() { console.log($(this).val() + " --> " + $(this).prop("checked")); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul class="multiselect-container dropdown-menu"> <li> <a tabindex="0"> <label class="checkbox"> <input type="checkbox" value="1001" />1001 </label> </a> </li> <li> <a tabindex="1"> <label class="checkbox"> <input type="checkbox" value="1201" />1201 </label> </a> </li> <li> <a tabindex="2"> <label class="checkbox"> <input type="checkbox" value="2001" />2001 </label> </a> </li> <li> <a tabindex="3"> <label class="checkbox"> <input type="checkbox" value="2200" />2200 </label> </a> </li> <li> <a tabindex="4"> <label class="checkbox"> <input type="checkbox" value="1004" />1004 </label> </a> </li> </ul> 

EDIT: 编辑:

As you are using a jQuery plugin to create the checkboxes , it is better to use the jQuery built-in event delegation to create the "change" event. 当您使用jQuery插件创建checkboxes ,最好使用jQuery内置事件委托来创建“ change”事件。 Something like this: 像这样:

var container = $(".multiselect-container");

container.on("change", "input", function() {

  console.log($(this).val() + " --> " + $(this).prop("checked"));

});

For this Bootstrap-multiselect jquery add on , there is a specific function to get the selected values. 对于此Bootstrap-multiselect jquery附加组件 ,有一个特定功能可获取所选值。

Here is the code: 这是代码:

<asp:ListBox ID="ListStoreID" CssClass="multiselect records" runat="server" SelectionMode="Multiple" DataSourceID="SqlDataSource3" DataTextField="ID" DataValueField="ID">                    
</asp:ListBox>

<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:recordsf1534 %>" SelectCommand="SELECT [ID] FROM [Store] WHERE ([Inactive] = @Inactive)">
    <SelectParameters>
        <asp:Parameter DefaultValue="false" Name="Inactive" Type="Boolean" />
    </SelectParameters>
</asp:SqlDataSource>

<script type="text/javascript">
    $(function () {
        $('[id*=ContentPlaceHolder1_ListStoreID]').multiselect({
            includeSelectAllOption: false,
            buttonText: function (options, select) {
                return 'StoreID';
            },
            buttonTitle: function (options, select) {
                var labels = [];
                options.each(function () {
                    labels.push($(this).text());
                });                  
                alert(labels.join(','));   // this alert will bring whenever user select the checkbox
                return labels.join(' , ');
            }
        });
    });
</script>

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

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