【问题标题】:Rails 4 : Input text box to search without submit button on index pageRails 4:输入文本框以搜索索引页面上没有提交按钮
【发布时间】:2016-02-18 07:02:17
【问题描述】:

在客户索引页面上的 Rails 应用程序中,我需要一个文本输入框,旁边没有提交按钮。我只想在其中输入客户编号,然后按回车按钮搜索应该可以工作。如何实现?

            <div class="row">
              <%= form_tag customers_path, :method => 'get', class: 'input-group'  do %>
                <%= text_field_tag :search, params[:search], class: 'form-control', placeholder: 'Search Customers' %>
                <span class="input-group-btn">
                  <%= submit_tag "search", class: "btn btn-success btn-md" %>
                </span>
              <% end %>
            </div>

我已将它放在客户编号列上方的客户页面的索引页顶部,因此我不希望此搜索框加上按钮在我的视图中占据很大的空间。所以我不需要提交按钮。或者有什么办法可以用右箭头按钮(bootstarp)替换搜索按钮,以节省我认为的空间。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4 ruby-on-rails-3.2


    【解决方案1】:

    您可以使用display: none; 隐藏您的提交按钮。表单仍然会在输入时提交。

    您还可以添加另一个带有图像的链接,并在单击链接时触发表单的提交。或者您甚至可以根据需要设置按钮样式。

    【讨论】:

      【解决方案2】:

      首先从你的代码中删除提交按钮,并在按下回车按钮时触发一个事件,就像这样。

          $('#search').keypress(function (e) 
             {
                if (e.which == '13') {
                  get the character typed in the text box and send the parameters
                  to controller-action thorugh ajax
                     }
                  });
      

      从这里获取参考如何将 ajax 发送到控制器操作

      Ruby on rails: How to send an AJAX request to rails controller from login popup along with user given credentials
      在您的控制器/动作中,它将是 从数据库执行选择操作

           select bla bla from xyz where column like %params reveived from ajax%
      
            return it to the calling ajax function
      

      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-05-14
        • 1970-01-01
        • 2016-08-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多