开发者

Get text field value with JQuery within ERB

I'm using Rails 3 and what I'm trying to do is to pass to autocomplete a value from another field which I'm going to use along with the input from the autocomplete field for filtering.

I have found out that I can pass a parameter with the autocomplete path:

 <%= rif.autocomplete_field :readable_qty , r开发者_StackOverflow中文版ecipes_autocomplete_weight_conversion_description_path + '?ingredient=' + $(this).attr(name), :'data-delimiter' => "\s" ,
              :value => @recipe.ingredients[i].readable_qty, :width=>30, :size=>20 %>

Here I'm trying to use $(this).attr(name) to get the current text from another text field which gives me error. Does anyone know how to make this work?


Looks like there's a gap in your understanding. I'm not a Rails user but I'm pretty sure the stuff inside <%...%> is evaluated on the server at the time the page is generated for return to the browser. This means that any expressions you use in there must be understood by rails, so putting a jquery expression in there will not work.

JQuery expressions on the other hand are evaluated in the browser as a result of some browser event (such as user clicking on a button). What it seems you are trying to do is capture browser information and then have it be acted on by the server. This requires either a get or a post to the server, causing the server to regenerate the page content or execute some action (based on your code between the <%...%>. I do that something like this:

$('#ingredient').click(function (e) {
    var postParams = {};
    postParams['ingredient'] = $(this).attr(name);
    $.post('myPage', 
    postParams,
...
});

This isn't working code. I'm just trying to give you the idea.

Then in your page code you pull the parameter out of the request instead of trying to use a jquery expression.

Hope this helps.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜