JQuery的:得到一个复选框的值/文本/ innerHTML的在ASP.NET CheckBoxList控件 [英] JQuery: getting the value/text/innerHtml of a checkbox in an ASP.NET CheckBoxList control

查看:67
本文介绍了JQuery的:得到一个复选框的值/文本/ innerHTML的在ASP.NET CheckBoxList控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆用asp.net CheckBoxList控件生成的复选框。我想要得到用户看到旁边的页面上的控件中的文本。

I've got a bunch of checkboxes generated by an asp.net CheckBoxList control. I want to get the text the user sees next to the control on the page.

通过类似的控制,如单选按钮列表,我已经能够通过这样做是为了得到他们的jQuery中值:

With similar controls, like RadioButtonList, I've been able to get their values in jQuery by doing this:

var selected = $("input:checked[id*='" + Control.id + "']");

,然后通过循环和获取值:

and then looping through and getting the value:

var whatIwant = selections[i].value;

(在这种情况下,价值将是我想要的文字)。

(In this case, that "value" will be the text I want).

但是 - 的CheckBoxList呈现不同。对于每一个列表项有没有只有输入,但一个html标记,像这样:

But - CheckBoxList renders differently. For each ListItem there's not only the input, but an html tag like so:

<input id="ctl00_ContentPlaceHolder1_ConsultedList_0" type="checkbox" name="ctl00$ContentPlaceHolder1$ConsultedList$0" />
<label for="ctl00_ContentPlaceHolder1_ConsultedList_0">Other service providers</label>

正如你所看到的,输入标签本身,这是我漂亮的小jQuery的查询发现,不包括我想要的信息:其他服务商。这就是标签上的标签。

As you can see, the input tag itself, which is what my nice little jQuery query finds, doesn't include the info I want: "Other service providers". That's on the label tag.

任何人都可以想到的一个很好的解决方案 - 制作的CheckBoxList渲染我需要在输入标签文本,或一些聪明的jQuery找到对应的选择的标签输入?

Can anyone think of a good solution for this - maybe a good way of making the CheckBoxList render the text I need in the input tag, or some clever jQuery to find the labels that correspond to the selected inputs?

推荐答案

您可以使用选择您已经使用(但只使用ClientID的),然后得到一个同级(标签),并获取其文本值

You can use the selector you are already using (but just use the clientId), then get the next sibling (the label) and get its text value

例如

$("#" + Control.ClientId).next().text();

或者你也可以通过获取标签它​​的属性。

Or you can just get the label by using its for attribute

$("label[for=" + Control.ClientId + "]").text();

这篇关于JQuery的:得到一个复选框的值/文本/ innerHTML的在ASP.NET CheckBoxList控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆