2012-07-27 34 views
0

我想为每个图像在页面上有一个评论表单。图像的数量是动态的。有没有办法让jquery针对点击提交按钮的表单数据?多种形式的目标点击按钮

我目前有:

$('.comment_button').click(function() {  
var comment = $('input#comment').val(); 
    var id = $('input#id').val(); 
    var other = $('input#other').val(); 
    var dataString = 'comment='+ comment + '&id=' + id + '&other=' + other; 

    alert (dataString); 
}); 

这意味着所有的表单字段和表单名称是为每个图像(或许应该让他们唯一的验证)相同,但不管有没有办法让表单字段当用户点击特定形式的某个提交按钮时?

回答

0

至于你的问题我的理解, 可以使用serialize函数,该函数 像

$('form').submit(function() { 
    console.log($(this).serialize()); 
    return false; 

});这里JquerySerialize

0

文档添加一个div容器的评论形式。因此,点击查找按钮的父div。然后找到该div中的所有输入元素,它将为该评论表单提供输入文本框。

甲样本伪代码..尝试这样..

$('.comment_button').click(function() {  

    var parentDiv= $(this).parent(); 
    $(parentDiv "input[type=text]").each(function() 
    { 
    if(this.id == "comment") 
     //get comment text 
    . 
    . 

    }); 
});