【问题标题】:jquery asp.net checkboxlist get item valuejquery asp.net checkboxlist 获取项目值
【发布时间】:2013-05-26 07:49:24
【问题描述】:

我正在尝试从 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>

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

$(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');
});

因此 optesttype 将具有 1 或 2,然后我尝试将其与 item[i] 值进行比较,但该值始终为“on”。 我用 var val 和 val2 尝试了我在网上找到的两种方法,但没有选择任何内容。 你们认为我需要如何处理这个问题? 谢谢,Laziale

【问题讨论】:

    标签: javascript jquery html asp.net webforms


    【解决方案1】:

    尝试以下操作:

    $("#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.");
            }
        });
    

    检查以下内容:

    ASP.NET CheckBoxList Operations with jQuery

    2)sample

    【讨论】:

      【解决方案2】:

      我通过以下方式解决了这个问题。我的复选框列表的 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>  
      

      生成的 HTML 如下所示

      <table id="ctl00_chkHourly" border="0">
      <TBODY>
       <TR>
       <TD>
        <INPUT id=ctl00_chkHourly_0 name=ctl00$chkHourly$0 value="" CHECKED type=checkbox>      
        <LABEL for=ctl00_chkHourly_0>00:00 AM</LABEL></TD>
       <TD>
        <INPUT id=ctl00_chkHourly_1 name=ctl00$chkHourly$1 value="" type=checkbox>
        <LABEL for=ctl00_chkHourly_1>01:00 AM</LABEL></TD>
       <TD>
        <INPUT id=ctl00_chkHourly_2 name=ctl00$chkHourly$2 value="" type=checkbox>
        <LABEL for=ctl00_chkHourly_2>02:00 AM</LABEL>
       </TD>
        </TR>
       </TBODY>
      

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

      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.");
       }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-07
        • 2013-09-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多