简体   繁体   中英

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

I'm trying to get the value from a textarea in my Haml file:

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

I have the JavaScript to handle this:

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

The problem is how do I call myFunction() such that my link_to first runs the function and the result is placed to the variable myreason ?

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

This is all inside a modal if that helps, and all of these are in the same show.html.haml .

On document ready get value of RejectionReason from textarea

Using JavaScript syntax:

: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;
  }

Using jQuery syntax:

: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)
  }

Add a id on the link and remove the reason parameter which will be passed using JavaScript:

= 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM