如何使用jQuery访问元素ID和其他属性?

如何使用jQuery访问元素ID和其他属性?

问题描述:

我有JavaScript方法时形成特定的类(MCB)提交其作用:如何使用jQuery访问元素ID和其他属性?

function BindCloseMessage() { 
    $(".mcb").submit(function(event) { 
     alert("closing..."); //Here I want to get the id of form 
     event.preventDefault(); 
    }); 
} 

代替警报呼叫,我需要访问形式,其提交叫的ID。我该怎么做?更妙的是将访问任何属性的提示...

感谢

提交表单的ID将

this.id  

或jQuery的

$(this).attr('id') //although why type the extra letters? 
+1

此外'''会被调用,这将创建一个新的'this'的jQuery对象,然后调用它的'attr'方法。 – Gumbo 2009-08-20 11:25:07

+0

确实,但我认为这是显而易见的;) – redsquare 2009-08-20 11:47:13

$(".mcb").submit(function(e){ 
    e.preventDefault(); 
    $(this).attr("id"); // Returns FORM ID 
}); 

您可以通过http://docs.jquery.com/Attributes

了解更多关于jQuery属性的方法