如何获取textarea的值并将其传递给link_to

时间:2019-06-04 03:43:28

标签: javascript ruby-on-rails ruby haml

我正在尝试从Haml文件中的textarea中获取值:

%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中。

1 个答案:

答案 0 :(得分:0)

准备好文档后,从RejectionReason获取textarea的值

使用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)
  }

在链接上添加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