Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/477.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在表单url中添加javascript中的id值?_Javascript_Html_Ruby On Rails - Fatal编程技术网

如何在表单url中添加javascript中的id值?

如何在表单url中添加javascript中的id值?,javascript,html,ruby-on-rails,Javascript,Html,Ruby On Rails,单击链接时,我们从date属性获取一个id,并将其添加到表单中,然后显示一个模式窗口 如何在表单中添加javascript中的id值 链接: = link_to _('Report this'), '#', class: 'report', data: { comment_id: comment.id, toggle: 'modal', target: "#report_reasons"} javascript: $(function() { $(".comments").on("cl

单击链接时,我们从date属性获取一个id,并将其添加到表单中,然后显示一个模式窗口

如何在表单中添加javascript中的id值

链接:

= link_to _('Report this'), '#', class: 'report', data: { comment_id: comment.id,   toggle: 'modal', target: "#report_reasons"}
javascript:

$(function() {
  $(".comments").on("click", ".comment a.report", function(e) {
    e.preventDefault();

    var $this = $(this);
    var commentId = $this.data("comment-id");
  });
});
表格:

需要从javascript插入数据attr,而不是2


谢谢。

最好在表单中维护一个隐藏字段作为id,并使用JavaScript设置值。我想你可以轻松地做到这一点

= simple_form_for(Report.new, url: report_video_comment_path(video.id, 2), remote: true) do |f|
  = f.hidden_field :comment_id
在JavaScript中:

$(function() {
  $(".comments").on("click", ".comment a.report", function(e) {
  e.preventDefault();

  $('#report_comment_id').val($(this).data("comment-id"));
  });
});

例如看起来怎么样?谢谢,用2怎么办?url:report\u video\u comment\u路径(video.id,2)。注释应为comment.ida,而不是2。当您使用嵌套管线时,此处必须有2个参数。在那里使用2作为伪值,在控制器中使用params[:report][:comment_id]来获取实际的注释
$(function() {
  $(".comments").on("click", ".comment a.report", function(e) {
  e.preventDefault();

  $('#report_comment_id').val($(this).data("comment-id"));
  });
});