如何在url中添加来自javascript的id值?

时间:2014-02-06 17:27:12

标签: javascript html ruby-on-rails

“当点击链接时,javascript我们从日期属性中获取一个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");
  });
});

形式:

= simple_form_for(Report.new, url: report_video_comment_path(video.id, 2), remote: true) do |f|

而不是2,有必要从javascript插入data-attr。

感谢。

1 个答案:

答案 0 :(得分:0)

更好地将表单中的隐藏字段维护为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"));
  });
});