jQuery的 - 使用自定义属性找到控制 [英] jQuery - find control using custom attribute

查看:77
本文介绍了jQuery的 - 使用自定义属性找到控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Asp.Net的CheckBoxList控件。为了获得选择复选框我是数据绑定到的CheckBoxList后为增加值自定义属性。

我能够得到的jQuery的选中的复选框,但我不知道如何使用自定义属性中的CheckBoxList找到特定的复选框。

这里是code:

在cs文件数据绑定后:

 的foreach(在cblRequestTypes.Items列表项LI)
        li.Attributes.Add(itemValue,li.Value);

要获取选中checboxes:

  $(:复选框),每个(函数(){。
              如果(this.checked){                selectedChecks + = $(本).parent()ATTR(itemValue)+〜。              }
            });

现在我传递的价值观在查询字符串,并基于我必须找到具有查询字符串中发送的itemValue属性的复选框。这部分不工作或可我在这里失去了一些东西。

  VAR ID = $ .QueryString [ID]
$(id.split(〜))。每个(函数(一,项目){
                $(复选框[itemValue =+项目+])ATTR(检查,检查)。            });

这是怎么了的CheckBoxList的HTML呈现:

 <跨度itemValue =3><输入ID =chkBoxList_0类型=复选框NAME =chkBoxList $ 0/><标签= chkBoxList_0>文字1 LT; /标签>< / SPAN>
<跨度itemValue =5><输入ID =chkBoxList_1类型=复选框NAME =chkBoxList $ 1/><标签=chkBoxList_1>文字2'; /标签>< / SPAN>
<跨度itemValue =6><输入ID =chkBoxList_2类型=复选框NAME =chkBoxList $ 2/><标签=chkBoxList_2>文字3'; /标签>< / SPAN>
<跨度itemValue =7><输入ID =chkBoxList_3类型=复选框NAME =chkBoxList $ 3/><标签=chkBoxList_3>文字4℃; /标签>< / SPAN>
<跨度itemValue =8><输入ID =chkBoxList_4类型=复选框NAME =chkBoxList $ 4/><标签=chkBoxList_4>文字5℃/标签>< / SPAN>
<跨度itemValue =9><输入ID =chkBoxList_5类型=复选框NAME =chkBoxList $ 5/><标签=chkBoxList_5>文字6≤/标签>< / SPAN>


解决方案

  $(跨度[itemValue = 8]输入)

这将让你在跨度,其中 itemValue (在这个例子中)为8的复选框。

我在我的评论说,这是一个有点混乱,你把你的价值属性上跨度元素,它似乎是混淆你,因为你的选择是 $(复选框[itemValue =+项目+]) - 这没有多大意义。

您选择将寻找一个复选框元素 - 它不存在;您正在寻找的元素是输入(使用的键入的复选框)。结果
它会试图找到一个具有 dataValue 属性 - 其中没有复选框做,因为这属性上的范围的<。 / p>

I am using CheckBoxList control of Asp.Net. In order to get selected checkboxes i am adding a custom attribute for Value after binding the data to the checkboxlist.

I am able to get the checked checkboxes in jQuery but i dont know how to find a particular checkbox using that custom attribute in the checkboxlist.

here is the code:

After databind in .cs file:

    foreach (ListItem li in cblRequestTypes.Items)
        li.Attributes.Add("itemValue", li.Value); 

To get the selected checboxes:

$(":checkbox").each(function() {
              if (this.checked) {

                selectedChecks += $(this).parent().attr("itemValue") + "~";

              }
            });

Now i am passing the values in the querystring and based on that i have to find the checkbox which has the itemValue attribute sent in the querystring. This part is not working or may be i am missing something here.

var id = $.QueryString["id"]
$(id.split("~")).each(function (i, item) {
                $("checkbox[itemValue=" + item +"]").attr('check',checked');

            });

This is how the HTML for CheckBoxList is rendered:

<span itemValue="3"><input id="chkBoxList_0" type="checkbox" name="chkBoxList$0" /><label for="chkBoxList_0">Text 1</label></span>
<span itemValue="5"><input id="chkBoxList_1" type="checkbox" name="chkBoxList$1" /><label for="chkBoxList_1">Text 2</label></span>
<span itemValue="6"><input id="chkBoxList_2" type="checkbox" name="chkBoxList$2" /><label for="chkBoxList_2">Text 3</label></span>
<span itemValue="7"><input id="chkBoxList_3" type="checkbox" name="chkBoxList$3" /><label for="chkBoxList_3">Text 4</label></span>
<span itemValue="8"><input id="chkBoxList_4" type="checkbox" name="chkBoxList$4" /><label for="chkBoxList_4">Text 5</label></span>
<span itemValue="9"><input id="chkBoxList_5" type="checkbox" name="chkBoxList$5" /><label for="chkBoxList_5">Text 6</label></span>

解决方案

​$("span[itemValue=8] input")​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

That'll get you the checkbox in the span where itemValue (in this example) is 8.

As I said in my comment, it's a little confusing that you're placing your value attribute on the span elements, and it seems to be confusing you too, since your selector is $("checkbox[itemValue=" + item +"]") - which doesn't make much sense.

Your selector will look for a checkbox element - which doesn't exist; the element you're looking for is input (with the type "checkbox").
And it'll try to find one that has the dataValue attribute - which none of the checkboxes do, since that attribute is on the span.

这篇关于jQuery的 - 使用自定义属性找到控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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