如何获取文本区域的值并将其传递给 link_to

How to get the value of a textarea and pass it to a link_to

我正在尝试从我的 Haml 文件中的文本区域获取值:

%p.text-left
  Please provide reason for rejection.
%textarea#RejectionReason.form-control{:rows => "3"}

我有 JavaScript 来处理这个问题:

:javascript
  function myFunction() {
    var x = document.getElementById("RejectionReason").value;
  }

问题是我如何调用 myFunction() 以便我的 link_to 首先运行该函数并将结果放入变量 myreason

= link_to (reject_job_order_path(@job_order, :reason => myreason), method: :get, class: 'btn btn-danger') do
  %i.fa.fa-thumbs-down
    Reject

如果有帮助的话,这一切都在模态中,所有这些都在同一个 show.html.haml

准备好文档时,从 textarea

获取 RejectionReason 的值

使用 JavaScript 语法:

:javascript
  if (document.readyState === 'complete') {
    var rejectReason = document.getElementById("RejectionReason").value;
    var currentRejectJobPath = document.getElementById("reject-job-link").href;
    var jobPathWithReason = currentRejectJobPath + "?reason=" + rejectReason;
    document.getElementById("reject-job-link").href=jobPathWithReason;
  }

使用 jQuery 语法:

:javascript
  $( document ).ready(function() {
    var rejectReason = $("#RejectionReason").val();
    var currentRejectJobPath = $("#reject-job-link").attr('href');
    var jobPathWithReason = currentRejectJobPath + "?reason=" + rejectReason;
    $("#reject-job-link").attr('href', jobPathWithReason)
  }

在 link 上添加一个 id 并删除将使用 JavaScript:

传递的 reason 参数
= link_to (reject_job_order_path(@job_order), method: :get, class: 'btn btn-danger', id: 'reject-job-link') do
  %i.fa.fa-thumbs-down
    Reject