2015年7月17日 星期五

jQuery - Get the closest form

Native DOM elements that are inputs also have a form attribute that points to the form they belong to:
var form = element.form;
alert($(form).attr('name'));
According to w3schools, the .form property of input fields is supported by IE 4.0+, Firefox 1.0+, Opera 9.0+, which is even more browsers that jQuery guarantees, so you should stick to this.
If this were a different type of element (not an ), you could find the closest parent with closest:
var $form = $(element).closest('form');
alert($form.attr('name'));

reference : http://stackoverflow.com/questions/991367/how-to-get-the-form-parent-of-an-input

沒有留言:

wibiya widget