jQuery backward selection
I have a form and i'm using this to select it's r开发者_如何学编程adio buttons:
$('form[id^="form-"]').find("input:radio");
But when I use a function on it I have to use this
(so that i'll know from which form the function is fired) and this
gives me form > radio-button
. How can I use this
to get the ID of the form?
The simplest and most efficient way is to access the form
property of the element in question:
alert(this.form.id);
To traverse from the input to its containing form and get the id, you would use:
$(this).closest('form[id^="form-"]').attr("id");
http://api.jquery.com/category/traversing/
If this
is a child element just do:
var form_id = $(this).parent().get(0).id;
//or
var form_id = $(this).parent().attr('id');
$(this).parent().attr('id');
parents()
is because I don't know if the inputs are direct descendants of form.
精彩评论