Ruby on Rails - passing the value of an input field to
Hi I am stuck here pretty bad. The senario is quite simple actually, I have an in开发者_StackOverflow社区put field like this:
%input.coupon_bar{:type => "text", :name=> "coupon", id=>"coupon_id"}/
And I want to pass whatever value is in that box to a different controller, I am trying to do it like this:
= link_to image_tag("ok_button.png", :border =>0), "/orders/new/", :coupon = #{$('.coupon_bar').val()}
I thought it would be simple using the jquery $('.coupon_bar').val()
but I guess its not as simple as I thought... any help please????
Oh, and unlike This StackOverFlow Question, my input field is not part of any form...
Thanks in advance...
I see there two bad practices:
Don't hardcode your links
Avoid obstrusive js when possible
Here is what I suggest:
= link_to image_tag("ok_button.png", :border =>0), new_order_path, :id => "my_link"
And in your js:
<script type="text/javascript" charset="utf-8">
$().ready(function(){
$('#my_link').click(function(){
$(this).attr('href', $(this).attr('href') + '?coupon=' + $('.coupon_bar').val());
});
});
</script>
Why don't you just put it in a form? That would be much easier.
Anyway, you can use the onclick from the link to achieve your goal:
<a href="#" onclick="window.location.href = '/orders/new/coupon='+ $('.coupon_bar').val();">
Text</a>
精彩评论