2012-05-24 51 views
0

我在我的頁面中使用了10個評論框。該按鈕的作品完美,但現在我想添加的功能,當我按下輸入按鈕,然後自動提交文字。集成提交按鈕與回車鍵按下評論框

如何在不更改代碼的情況下添加此功能。我將事件綁定到bsubmit類上單擊。 (有10個評論框)

<div class="addcomment" style="display: block; "> 
<input class="commentadd" type="text" name="comment" value="Enter Comment" onfocus="if(this.value=='Enter Comment')this.value='';"> 
<button class="bsubmit" type="button">Submit</button></div> 

事件代碼

$(document).ready(function() { 

$(function() { 
$(".bsubmit").live("click", function() { 
var id = $(this).parent().parent().attr("id"); 
var comm= document.getElementById(id).getElementsByClassName("commentadd")[0].value; 


    $.ajax({ 
    type: "POST", 
    url: "comment.php", 
. 
. 
. 

回答

5
$('body').on('keyup','input.commentadd', function(e) { 
    // e.which is monitoring the key pressed 
    // and 13 is code for enter key 
    if(e.which == 13 && $.trim(this.value).length) { 
    $(this).next(".bsubmit").click(); // triggering click event on button 
    } 
}); 

DEMO

+0

但我bsubmit.click是有 > VAR ID = $(本).parent ().parent()ATTR( 「ID」)。 所以在這種情況下,$(this)會被調用?哪個按鈕?它會調用同一個嗎?我有10個評論框 – Yahoo

+0

@AdiMathur按鈕的$(this),因爲它觸發按鈕事件,並且所有執行都將在按鈕的範圍內執行 – thecodeparadox

+0

我試過了,調用了所有的按鈕提交。 :) - - - 每個評論框提交了默認評論「輸入評論」 – Yahoo