简体   繁体   中英

Is it possible to grab a value from an input, and pass it in the params of a ruby link_to

I have a input fields, and if the user changes an input, I want it to reflect on the next page.

This is what the input looks like:

<input value="<%= @premium_discount.round %>" class="store_pricing_text_box store_gold" id="pro">

And then the link to go to the next page already passes a ruby param of the current item , but I'd also like to pass the value in the input with id pro

<%= link_to "I want this!", new_purchase_path(item_id: @item.id), class: "btn_primary" %>

I thought doing inline jQuery might be the solution - but that doesn't seem to work in a ruby line. Any other thoughts?

Thank you!

You could add another a class (or an id ) to the link_to and a click event to append the extra parameters; for example:

<%= link_to "I want this!", new_purchase_path(item_id: @item.id), class: "btn_primary my-link" %>

<script>
  $('.my-link').click(function() {
    $(this).attr("href", this.href + "&pro=" + $('#pro').val());
  });
</script>

Notice the added my-link class which is used by jQuery to detect the click on the link and add the pro attribute at the end.

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