开发者

How to get the form ID attribute in a delegate event

开发者 https://www.devze.com 2023-01-19 19:03 出处:网络
$(\'form[role=form]\').delegate( \"input[role=submit_button]\", \"click\", function() { alert( FORM.ID); /// ????????????
$('form[role=form]').delegate( "input[role=submit_button]", "click", function() {
    alert( FORM.ID  ); /// ????????????
});

IMPORTANT: without use closest() or parent() ... you know, when you write $('form[role=for开发者_运维知识库m]') here you have the element finded ... why to search it newly ???


this.form.id should do it ...

explanation

All input elements (input, select, button etc) that are contained in a form tag, keep a reference to that container in the .form property.


You cannot do whitout search it again....

alert($(this).closest("form[role=form]").attr("id"));


In that function this and $(this) will refer to input[role=submit_button] if you want a reference to the form you should save it in a variable before calling delegate so:

var myForm = $('form[role=form]');
myForm.delegate( "input[role=submit_button]", "click", function() {
    alert( myForm.attr("id")  ); /// ????????????
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号