x-editable on按钮,如何编辑特定注释

x-editable on button, how to edit specific comment?

本文关键字:编辑 注释 按钮 on 何编辑 x-editable      更新时间:2023-09-26

我已经添加了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为"编辑评论"的第一条评论。当我在循环中创建下一个注释时,问题就开始了,而这个注释具有相同的id:),触发器上的第二个编辑按钮如何知道编辑哪个注释(对象)?

好的,我得到了解决方案。"找到按钮的父对象,然后找到带有类编辑注释的迭代对象,并在该元素上使用切换"

当我理解这个逻辑时,我只需要使用正确的语法,所以:

$(function() {  
    $('.edit-button').click(function(e) {
      e.stopPropagation();
      $(this).parent().find(".edit-comment").editable('toggle');
    });
});