如何将具有相同类名的div彼此分开?我有这样的事情:
<form role="form" id="comment-form">
<textarea class="form-control" rows="1" placeholder="{% trans 'Write a comment...' %}" name="comment" id="comment"></textarea>
</form>
<form role="form" id="comment-form">
<textarea class="form-control" rows="1" placeholder="{% trans 'Write a comment...' %}" name="comment" id="comment"></textarea>
</form>
<form role="form" id="comment-form">
<textarea class="form-control" rows="1" placeholder="{% trans 'Write a comment...' %}" name="comment" id="comment"></textarea>
</form>
...
我想补充一点:
$("#comment").keydown(function (evt) {
var keyCode = evt.which?evt.which:evt.keyCode;
if (evt.ctrlKey && (keyCode == 10 || keyCode == 13)) {
$.ajax({
url: '/articles/comment/',
data: $("#comment-form").serialize(),
cache: false,
type: 'post',
但是在keydown上只有第一个textarea,id =&#34;评论&#34;响应,因为每个元素必须有unice id。我尝试将它们改为课程,但随后他们会全部回复。我怎么能把这些形式分开,不进入彼此的方式?
答案 0 :(得分:0)
您需要将ID更改为类,这样您就不会重复ID,然后在定位.comment-form
时需要引用每个块中的相对类,&& #39; .comment&#39;等
$(".comment").keydown(function (evt) {
var keyCode = evt.which?evt.which:evt.keyCode;
if (evt.ctrlKey && (keyCode == 10 || keyCode == 13)) {
var $commentForm = $(this).closest(".comment-form"),
$comment = $(this);
$.ajax({
url: '/articles/comment/',
data: $commentForm.serialize(),
cache: false,
type: 'post',
success: function (data) {
/* comment-list and comment-count aren't in your html...
$("#comment-list").html(data);
var comment_count = $("#comment-list .comment").length;
$(".comment-count").text(comment_count);
*/
$comment.val("");
$comment.blur();
}
});
}
});
&#13;
<form role="form" class="comment-form">
<textarea class="form-control comment" rows="1" placeholder="{% trans 'Write a comment...' %}" name="comment"></textarea>
</form>
<form role="form" class="comment-form">
<textarea class="form-control comment" rows="1" placeholder="{% trans 'Write a comment...' %}" name="comment"></textarea>
</form>
<form role="form" class="comment-form">
<textarea class="form-control comment" rows="1" placeholder="{% trans 'Write a comment...' %}" name="comment"></textarea>
</form>
&#13;