javascript jquery asp.net checkboxlist 获取项目值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16841207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 06:11:53  来源:igfitidea点击:

jquery asp.net checkboxlist get item value

javascriptjqueryhtmlasp.netwebforms

提问by Laziale

I am trying to get the values from checkboxlist in jquery and depending from which value there is, make the checkbox checked or unchecked. This is what I have:

我试图从 jquery 中的复选框列表中获取值,并根据存在的值,选中或取消选中复选框。这就是我所拥有的:

  <asp:CheckBoxList CssClass="styled" ID="chkTestTypeEdit" RepeatDirection="Horizontal" style="padding:5px;" runat="server">
       <asp:ListItem Value="1" Text="Y/N" />
       <asp:ListItem Value="2" Text="Num" />
  </asp:CheckBoxList>
  <asp:CheckBoxList CssClass="styled" ID="chkTestTypeEdit" RepeatDirection="Horizontal" style="padding:5px;" runat="server">
       <asp:ListItem Value="1" Text="Y/N" />
       <asp:ListItem Value="2" Text="Num" />
  </asp:CheckBoxList>

Then before modal popup is open I have this piece of code:

然后在模态弹出窗口打开之前我有这段代码:

$(document).on("click", ".open-EditTest", function () {                 
    var optesttype =$(this).data('optesttype');                
    var items = $('#<% = chkTestTypeEdit.ClientID %> input:checkbox');
    for (var i = 0; i < items.length; i++) {

        var val = $('#ctl00_MainContent_chkTestTypeEdit_0').val();
        var val2 = $('label[for=" + <%= chkTestTypeEdit.ClientID %> +_0 "]').text();
        if (items[i].value == optesttype) {
            items[i].checked = true;
            break;
        }
    }
    $('#EditTest').modal('show');
});

So the optesttype will have either 1 or 2, and then I am trying to compare that agains the item[i] value but that value is always 'on'. I tried two methods I found on the net with var val and val2, but nothing gets selected. How do you guys think I need to approach this? Thanks, Laziale

所以 optesttype 将有 1 或 2,然后我试图将其与 item[i] 值进行比较,但该值始终为“on”。我尝试了两种我在网上找到的 var val 和 val2 方法,但没有被选中。你们认为我需要如何处理这个问题?谢谢,拉齐亚莱

回答by Kaushik Ghosh

I solved the problem in the following way. My ASP.NET code for the checkboxlist is as below

我通过以下方式解决了这个问题。我的复选框列表的 ASP.NET 代码如下

<asp:CheckBoxList ID="chkHourly" runat="server" RepeatLayout="Table" 
RepeatColumns="4"   RepeatDirection="Horizontal">
 <asp:ListItem Value="0">00:00 AM</asp:ListItem>
 <asp:ListItem Value="1">01:00 AM</asp:ListItem>
 <asp:ListItem Value="2">02:00 AM</asp:ListItem>
</asp:CheckBoxList>  

The generated HTML will look like this below

生成的 HTML 如下所示

<table id="ctl00_chkHourly" border="0">
<TBODY>
 <TR>
 <TD>
  <INPUT id=ctl00_chkHourly_0 name=ctl00$chkHourly
var postData = new Array();
$("[id*=chkHourly] input[type=checkbox]:checked").each(function () {
     alert($(this).next().text());
     alert($(this).next().html());
     alert($(this).attr("name"));
     postData.push($(this).next().text());
 });

 if (postData.length > 0) {
  alert("Selected Text(s): " + postData);
 } 
 else {
  alert("No item has been selected.");
 }
value="" CHECKED type=checkbox> <LABEL for=ctl00_chkHourly_0>00:00 AM</LABEL></TD> <TD> <INPUT id=ctl00_chkHourly_1 name=ctl00$chkHourly value="" type=checkbox> <LABEL for=ctl00_chkHourly_1>01:00 AM</LABEL></TD> <TD> <INPUT id=ctl00_chkHourly_2 name=ctl00$chkHourly value="" type=checkbox> <LABEL for=ctl00_chkHourly_2>02:00 AM</LABEL> </TD> </TR> </TBODY>

Please notice that there is a label created besides each input in the table, and when a checkbox is checked, the input's value will be 'on' and what you see as an option is the label's text, in my case I needed the text, but to get the value also in a round about away, I would read the name of the individual input fields that are checked. Please see the code below that I have written to read the text selected and also the name of the input selected so that I can strip it and read the value if needed.

请注意,除了表中的每个输入之外,还有一个标签创建的标签,当检查复选框时,输入的值将为“开”以及您认为选择的内容是标签的文本,在我需要的情况下,我需要的文本,但为了获得该值,我会读取所检查的各个输入字段的名称。请参阅下面我编写的代码以读取所选文本以及所选输入的名称,以便我可以在需要时剥离它并读取值。

$("#demo").live("click", function () {
        var selectedValues = "";
        $("[id*=CheckBoxList1] input:checked").each(function () {
            if (selectedValues == "") {
                selectedValues = "Selected Values:\r\n\r\n";
            }
            selectedValues += $(this).val() + "\r\n";
        });
        if (selectedValues != "") {
            alert(selectedValues);
        } else {
            alert("No item has been selected.");
        }
    });

回答by Kaushik Ghosh

Try following:

尝试以下操作:

##代码##

Check following:

检查以下内容:

ASP.NET CheckBoxList Operations with jQuery

使用 jQuery 的 ASP.NET CheckBoxList 操作

2) sample

2)样品