jQuery - find control using custom attribute

六月ゝ 毕业季﹏ 提交于 2019-12-23 04:35:23

问题


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>

回答1:


​$("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.




回答2:


Someone else already answered this, but when i tried to upvote the answer it appeared to have been deleted.

Try:

$('span[itemValue=8] input')

(where 8 is the id you want to look for; happened to pick 8 for this example)



来源:https://stackoverflow.com/questions/9386810/jquery-find-control-using-custom-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!