2013-04-17 77 views
1

我有這樣一段代碼insted的使用提交按鈕發送的回車鍵

// watch textarea for release of key press 
     $('#sendie').keyup(function(e) { 
      if (e.keyCode == 13) { 
       var text = $(this).val(); 
       var maxLength = $(this).attr("maxlength"); 
       var length = text.length; 
       // send 
       if (length <= maxLength + 1) { 
        chat.send(text, name); 
        $(this).val(""); 
       } else { 
        $(this).val(text.substring(0, maxLength)); 
       } 
      } 
     }); 

它是什麼,它曾經有人打回車鍵發送在一個文本輸入到某個電子郵件地址的消息。我如何改變這個使用下面的代碼中的提交按鈕?

< div id="page-wrap"> 
     < form id="send-message-area"> 
      < p>Your message: </p> 
      < textarea id="sendie" maxlength = '100' placeholder ="Send Your Message"> < /textarea> 
    < input type="submit"> 
     < /form> 

    < /div> 

回答

1
$("#send-message-area").on("submit", function (e) { 
var text = $("#sendie").val(); 
var maxLength = $("#sendie").attr("maxlength"); 
var length = text.length; 
// send 
if (length <= maxLength + 1) { 
     chat.send(text, name); 
    alert(text); 
     $("#sendie").val(""); 
} else { 
     $("#sendie").val(text.substring(0, maxLength)); 
} 
e.preventDefault(); 
}); 

檢查樣品here

2

因此,代碼附加到提交表單的

$("#send-message-area").on("submit", function (e) { 
    var elem = $("#sendie"); 
    var text = elem.val(); 
    var maxLength = parseInt(elem.attr("maxlength"),10); 
    var length = text.length; 
    // send 
    if (length <= maxLength + 1) { 
      chat.send(text, name); 
      elem.val(""); 
    } else { 
      elem.val(text.substring(0, maxLength)); 
    } 
    e.preventDefault(); 
}); 
+0

非常感謝你:) – Mardwan