从 jquery 中的 asp 复选框列表中获取选定复选框的值
我正在尝试获取复选框列表的选定值(或多个值)。我的问题是,我在 c# 中绑定复选框列表,并且它没有使用“Value”属性进行渲染,就像我硬编码一样,
<asp:ListItem Value=".." .. />
我的复选框列表如下所示:
<asp:CheckBoxList runat="sever" ID="cblStuff" DataValueField="myID" DataTextField="myName"></asp:CheckBoxList>
因此,当我尝试使用 jquery 并执行以下操作时,它仅返回“on”而不是“myID”。我错过了什么吗?我的印象是 DataValueField 的用途是什么? 这是我正在使用的js:
$("checkboxlist selector").change(function() {
$(this).find(":checked").each(function() { alert($(this).val()); });
});
谢谢。
I am trying to get the selected value (or values) of the checkboxlist. My problem is that I am binding the checkboxlist in c# and it's not rendering with the "Value" attribute as it would if I were to hard code the
<asp:ListItem Value=".." .. />
My checkboxlist looks like this:
<asp:CheckBoxList runat="sever" ID="cblStuff" DataValueField="myID" DataTextField="myName"></asp:CheckBoxList>
So when I try to use jquery and do the follow, it returns only "on" as apposed to "myID". Am I missing something? I was under the impression that is what the DataValueField was for?
Here is the js I am using:
$("checkboxlist selector").change(function() {
$(this).find(":checked").each(function() { alert($(this).val()); });
});
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
DataValueField 是服务器端属性,它不会在 html 中呈现,因此您无法使用 jquery 或任何其他客户端代码获取该值。
另请检查相关问题: Where are the DataValueField value for a CheckBoxList已存储?
DataValueField is server side property and it will not be rendered in html , so you cannot get the value with jquery or any other client side code.
also check the related question: Where are the DataValueField values for a CheckBoxList stored?
您应该能够获取所有选中的项目,如下所示:
我认为
CheckBox
控件默认没有 value 属性,但CheckBoxList
可能有。无论哪种情况,您始终可以添加值属性。You should be able to get all of the checked items like this:
I don't think the
CheckBox
control has a value attribute by default, but theCheckBoxList
might. In either case, you can always add a value attribute.您可以使用 jQuery 的
attr()
方法来获取任何属性,无论是官方的还是其他的:You can use jQuery's
attr()
method to get any attribute, official or otherwise:这将检查该值是否已设置为
DesiredValue
。每当值更改时就会触发该事件。您必须检查以确保导致事件触发的项目已被选中,如果是,则它是否具有您关心的值。This will check to see if the value has been set to
DesiredValue
. The event fires whenever the value is changed. You must check to make sure that the item causing the event to fire is checked, and if it is if it has the value you care about.