0

I have about 20 forms in my web page

<form id="post_comment_1" class="post_comment" action="cmt.php" method="post">

// form inputs

</form>

.......

<form id="post_comment_20" class="post_comment" action="cmt.php" method="post">

// form inputs

</form>

Now, I have to track the form submit using javascript. if there is only one form having id (post_comment) I can track it like this

$("#post_comment").submit(function(event){

    // function statements    

});

But I can not do this because I have about 20 forms on one page. What is the efficient way to have an event listener on each of the form submit??

4

2 回答 2

4
$('.post_comment').submit(function(evt) {})
于 2013-04-22T06:48:03.293 回答
1

尝试使用此属性选择器^将选择以给定字符串开头的所有 id..

 $("form[id^='post_comment'").submit(function(event){
      .....

或班级

$('.post_comment').submit(function(event) {
  .....  

类选择器比属性选择器更好更快..所以我会选择类选择器..(如果你可以在你的表单中添加一个类)

来自文档

attributeStartsWith 选择器:但是它会比使用类选择器慢,因此如果可以的话,可以利用类来对类似元素进行分组。

于 2013-04-22T06:52:41.887 回答