gpt4 book ai didi

jquery - 使用 jQuery 获取数据绑定(bind) asp.NET 复选框列表的最后选定值

转载 作者:行者123 更新时间:2023-12-01 03:45:42 25 4
gpt4 key购买 nike

我在 asp.NET 中有一个带有 DataTextField 和 DataValueField 的数据绑定(bind)复选框列表。

现在,如果我选择该列表中的单个项目,我只需要该项目的值。

我找到了很多解决方案来查找所有选定项目的值,但这不是我需要的。 我唯一需要的值是我刚刚选择或取消选择的值。

有没有办法用 jQuery 代码获取这个值?例如,在如下代码所示的警报中。

$("#checkboxlist").click(function () {
alert('value of the item that just got (de)selected');
});

非常感谢!

更新:这是html部分:

<span id="checkboxlist">
<input id="checkboxlist_0" type="checkbox" name="checkboxlist$checkboxlist_0" value="11" />
<label for="checkboxlist_0">Item 1</label><br />

<input id="checkboxlist_1" type="checkbox" name="checkboxlist$checkboxlist_1" value="12" />
<label for="checkboxlist_1">Item 2</label><br />

<input id="checkboxlist_2" type="checkbox" name="checkboxlist$checkboxlist_2" value="13" />
<label for="checkboxlist_2">Item 3</label><br />

<input id="checkboxlist_3" type="checkbox" name="checkboxlist$checkboxlist_3" value="14" />
<label for="checkboxlist_3">Item 4</label><br />
</span>

最佳答案

此代码应该可以工作( Attribute Ends with selector ):

$('[id$="checkboxlist"] input').click(function () {
alert($(this).val());
});

asp:CheckBoxList 将呈现为某个元素(在您的情况下,它应该是 span),其 ID 类似于 ctl00_contenttop_checkboxlist 以及其中的输入。上面的代码将查找 ID 以 checkboxlist 结尾的元素内的所有输入,并在单击其中一个元素时触发。

但是属性结尾可能会很慢,所以最好这样做:

$("#<%=checkboxlist.ClientID %> input").click(function () {
alert($(this).val());
});

checkboxlist.ClientID 将返回客户端包装元素的 ID。

更新:

根据您的 HTML,看起来不需要 ClientID(因为看起来您正在使用启用了 ClientIDMode 的 .net 4)。在这种情况下,您可以简单地使用:

$("#checkboxlist input").click(function () {
alert($(this).val());
});

Demo

关于jquery - 使用 jQuery 获取数据绑定(bind) asp.NET 复选框列表的最后选定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13723148/

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