javascript - x-editable按钮,如何编辑特定评论?

标签 javascript jquery ruby-on-rails ruby-on-rails-4 x-editable

我添加了 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/

相关文章:

javascript - 删除Attr()问题

javascript - 这段 javascript 代码哪里出错了?

jquery - 使用 Bootstrap 显示然后淡入淡出元素

ruby-on-rails - 搜索功能寻找多个单词

javascript - 在页面渲染之前运行 Javascript 和 Controller 操作

php - 如果您的弹出窗口被阻止,如何获得通知?(HTML JS PHP)

javascript - 将时间戳添加到 Google Spreadsheet onEdit 脚本中的命名列

jquery - 仅滚动子元素

javascript - jQuery 工具提示添加换行符

ruby-on-rails - 乘客/Nginx错误 "unexpected end of file detected."