如何获取 text_field rails 的值 [英] How to Get value of text_field rails

查看:41
本文介绍了如何获取 text_field rails 的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本字段:

<%= text_field :search, :class=>'input-xxlarge search-query',:id=>'keyword' %>

然后单击一个链接,我想将此文本字段中的值传递给控制器方法.我不想使用表单和表单提交.

Then on click of a link I want to pass the value in this text field to a controller method. I do not want to use a form and form submit.

我的链接是:

<a href ="/home/search" >GO</a>

对于这个搜索"方法,我想传递文本字段值....也可以直接跳转到页面/home/search"专为此动作搜索"

and to this 'search' method I want to pass the text field value.... also to go directly to the page "/home/search" designed to this action "search"

我该怎么做???谢谢...

How do I do this??? Thank you...

推荐答案

阅读这里 link_to 发送参数和 url 并获取它们目标页面

<%= link_to "Go", '/home/search?param1=value' %>

因此,如果您不使用表单,则应使用 jQuery 将字段值放入带有参数的属性链接 (href) 中.jsffidle

So if you won't use form, you should use jQuery for put value of field into attribute link (href) with parameter. Example on jsffidle

<%= text_field :search, :class=>'input-xxlarge search-query',:id=>'keyword' %>
<%= link_to "Go", '', :id => "searchlink" %>


$(':input').bind('keypress keydown keyup change',function(){
    var word = $(':input[id="keyword"]').val();
    $('a[id="searchlink"]').attr("href","/home/search?param1=" + word.toString());

});

在控制器中:

if params[:param1] == ""
 render :search # or redirect whatever do you want 
else
  param = params[:param1]
  ....
end

这篇关于如何获取 text_field rails 的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆