gpt4 book ai didi

javascript - jquery asp.net 复选框列表获取项目值

转载 作者:行者123 更新时间:2023-11-28 01:56:12 24 4
gpt4 key购买 nike

我正在尝试从 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 尝试了在网上找到的两种方法,但没有选择任何方法。你们认为我需要如何处理这个问题?谢谢,拉齐亚莱

最佳答案

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

关于javascript - jquery asp.net 复选框列表获取项目值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16841207/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com