我添加了 x-editable 注释来编辑按钮,但我不知道编辑按钮如何找到编辑哪个对象?
<% commentable.comments.each do |comment| %>
<div class="well">
<a href="#" data-xeditable="true" data-pk="<%= comment.id %>"
style="border:none; color:black; text-decoration: none !important;cursor: default;"
id="edit-comment"
data-model="comment"
data-name="content"
data-type="text"
method="put"
data-toggle="manual"
data-url="<%= comment_path(comment) %>">
<%= comment.content %>
</a>
<div class="btn btn-default" id="edit-button" style="float:right;">
<span class="glyphicon glyphicon-edit" aria-hidden="true"> Edit </span>
</div>
</div>
我的JS:
/* X-Editable*/
$(function() {
$.fn.editable.defaults.mode = 'inline';
return $("[data-xeditable=true]").each(function() {
return $(this).editable({
ajaxOptions: {
type: "PUT",
dataType: "json"
},
params: function(params) {
var railsParams;
railsParams = {};
railsParams[$(this).data("model")] = {};
railsParams[$(this).data("model")][params.name] = params.value;
return railsParams;
}
});
});
});
$(function() {
$('#edit-button').click(function(e) {
e.stopPropagation();
$('#edit-comment').editable('toggle');
});
});
结果是:当我点击编辑按钮时,我可以编辑第一个评论,其ID为:“edit-comment”。当我在循环中创建下一条评论并且该评论具有相同的 id 时,问题就开始了:) 触发器上的第二个编辑按钮如何知道哪个评论(对象)编辑?
最佳答案
好的,我找到了解决方案。 “找到按钮的父级,然后找到带有编辑注释类的迭代对象,并在该元素上使用切换”
当我理解这个逻辑时,我只需要使用正确的语法,这样:
$(function() {
$('.edit-button').click(function(e) {
e.stopPropagation();
$(this).parent().find(".edit-comment").editable('toggle');
});
});
关于javascript - x-editable按钮,如何编辑特定评论?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37030537/