Javascript 使用具有多个id的jquery提交评论表单
我在jquery中有以下表单Javascript 使用具有多个id的jquery提交评论表单,javascript,jquery,html,node.js,forms,Javascript,Jquery,Html,Node.js,Forms,我在jquery中有以下表单 ' <form class="form comment_form" role="form" id="'+this._id+'" name="comment-form" action="/users/reply" method="post">'+ '<div class="form-group">'+ '<input type="hidden" name="post_id" value=
' <form class="form comment_form" role="form" id="'+this._id+'" name="comment-form" action="/users/reply" method="post">'+
'<div class="form-group">'+
'<input type="hidden" name="post_id" value="'+this._id+'"/> ' +
'<input class="form-control col-lg-12 red" style="width:100%" type="text" name="user_comment" placeholder="Your comments" />'+
'</div>'+
'<div class="form-group">'+
'</div>'+
'</form>' +
“”+
''+
' ' +
''+
''+
''+
''+
'' +
我正在尝试提交表单,但它有一个特定的id。表单只是一个输入字段,我只想在用户按enter键时提交它。类似于Facebook。我的问题是,如何让用户只需按enter按钮即可提交特定表单?id必须是唯一的。此外,带有submit按钮的表单在按下enter键时可用于触发
submit
'<form class="form comment_form" role="form" id="' + this._id + '" name="comment-form" action="/users/reply" method="post">' +
'<div class="form-group">' +
'<input type="hidden" name="post_id" value="input_' + this._id + '"/> ' +
'<input class="form-control col-lg-12 red" style="width:100%" type="text" name="user_comment" placeholder="Your comments" />' +
'</div>' +
'<div class="form-group">' +
'</div>' +
'<div><button type="submit">Submit</button></div>'+
'</form>'
我成功地找到了我问题的答案。我所做的是使用this关键字查找表单的其他元素
var form = $('form.comment_form');
form.on('submit',function(e){
e.preventDefault();
var input = $(this).find('input[type=text]'),
comment_id = $(this).find('input[name=post_id]');
})
所有表单id都是唯一的。(id=“”+这个。_id+”)只需在代码中添加一个
,它将在Enter buttonThanks@ManishYadav上提交表单,但我尝试使用jquery提交表单,因此当按下Enter按钮时,页面不会刷新。我尝试使用e.which语句,但选择了我不熟悉的特定表单。感谢您的回复@brk似乎是一个好消息很好,但我如何找到表单的特定id再次感谢该解决方案,它帮助我找到了正确的方法。我找到了表单的类名,并使用this关键字查找表单的其他元素
var form = $('form.comment_form');
form.on('submit',function(e){
e.preventDefault();
var input = $(this).find('input[type=text]'),
comment_id = $(this).find('input[name=post_id]');
})