开发者

Ruby on Rails - passing the value of an input field to

开发者 https://www.devze.com 2023-02-23 10:21 出处:网络
Hi I am stuck here pretty bad. The senario is quite simple actually, I have an in开发者_StackOverflow社区put field like this:

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:

  1. Don't hardcode your links

  2. 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>
0

精彩评论

暂无评论...
验证码 换一张
取 消