简体   繁体   中英

jquery to select parents node

To find form element from the element which triggered event. I use following code. but it seems crazy to me.

Is there a better way doing this?

$("#xx").click(function(event) {
    var testValue = $(this).parent().parent().parent().parent().parent().parent().parent().data("test");
});

thank you in advance.

Crazy is right. :D

$(this).closest('#firstForm') should do this for you.

If your HTML is valid then simply do

$("#firstForm") 

ids are unique (assuming valid HTML)

$("#xx").click(function(event) {
    $(this).parents('#YourId').attr("firstForm");
});

for reference see this link : http://api.jquery.com/parents/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM