我在我的页面中使用了 10 个评论框。该按钮工作完美,但现在我想添加功能,当我按下输入按钮时,会自动提交文本。
如何在不更改太多代码的情况下添加此功能。我将 bsubmit 类与单击事件绑定(bind)。 (共有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",
.
.
.
最佳答案
$('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
关于javascript - 将提交按钮与评论框的 Enter 按键集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10743090/