我在我的rails 3应用程序中有一个搜索表单。此表单有一些下拉菜单,用于设置不同的搜索标准。然后,表单根据所选内容搜索一个名为"item“的模型。这将导致创建并分页的项列表。这是我使用的原始表单标记:
<%= form_tag("", :method => "post") do %>然后,我想将一个" ajax“侦听器附加到一个下拉菜单(为了影响另一个下拉菜单),我仍然希望使用POST (用于”普通“搜索)提交表单的其余部分。ajax部分仅用于显示不同的搜索条件。我试过这个表格标签:
<%= form_for(:itemsbylocation, :remote => true) do |f| %>不幸的是它破坏了某种形式的东西..。我不能再用帖子提交了。不过,它会触发一个简单的javascript (来自js模板),这只会发出警报。
所以..。表单一开始是一个简单的表单,使用POST,没有ajax,而且效果很好。然后,我想要ajax的一小部分表单,只是一个或两个下拉,所以我尝试了另一种类型的形式使用“远程”。我仍然希望主要搜索(项目)使用POST (没有Ajax).在这种情况下,有人有什么建议吗?提前感谢!
编辑
对于我的下拉列表,我决定绕过rails ajax "remote“方式,也就是说,我不使用"remote",甚至不使用表单。也许这样做有一些缺点..。比如安全问题?,但这对我有用。我刚刚将这个javascript (侦听器)代码(参见下面)添加到一般的javascript中。我添加了一个新的控制器操作(类别/类别更改)、一个路由和一个js-模板。在javascript视图中,我可以根据需要操作下拉列表。..。
$("#type").change(function(){
var catValue = $(this).val();
var formaction = "/categories/category_changed/"+catValue;
args = "id="+catValue;
$.ajax({
type: "GET",
url: formaction,
data: args,
dataType: "script"
})
});..。
编辑的端
一些密码..。
部分:
<%= form_for(:itemsbylocation, :remote => true) do |f| %>
<label class="left search" for="search-inp">
<%= f.text_field :searchstr %>
</label>
<select class="custom" id="type" name="cat">
<option value=''>Choose category</option>
<% @super_categories.each do |supercat| %>
<option class="optgroup"><%= supercat.name %></option>
<% supercat.category.each do |cat| %>
<option <%= highlight_if_selected params[:cat], cat.id %> value='<%= cat.id %>'><%= cat.name %></option>
<% end %>
<% end %>
</select>
<select name="loc" class="custom">
<option value=''>Choose location</option>
<% @locations.each do |location| %>
<option <%= highlight_if_selected @selected_location, location.id %>
value='<%= location.id
%>'><%= location.name %></option>
<% end %>
</select>
<div class="clr h-small"></div>
<div id="buttonset">
<input type="checkbox"
<% if get_mode(params,
@modes['sell']) %>
checked="checked" <% end %>
value="<%= @modes['sell'] %>" name="mode_id_<%= @modes['sell'] %>" id="search-1" class="checkbox" />
<label class="checkbox" for="search-1">Selling</label>
<input type="checkbox" <% if get_mode(params,
@modes['buy']) %>
checked="checked" <% end %>
value="<%= @modes['buy'] %>" name="mode_id_<%= @modes['buy'] %>" id="search-2" class="checkbox" />
<label class="checkbox" for="search-2">Buying</label>
<input type="checkbox" <% if get_mode(params,
@modes['to_rent']) %>
checked="checked" <% end %>
value="<%= @modes['to_rent'] %>" name="mode_id_<%= @modes['to_rent'] %>" id="search-3" class="checkbox" />
<label class="checkbox"
for="search-3">Renting</label>
<input type="checkbox" <% if get_mode(params,
@modes['wish_to_rent']) %>
checked="checked" <% end %>
value="<%= @modes['wish_to_rent'] %>" name="mode_id_<%= @modes['wish_to_rent'] %>" id="search-4" class="checkbox" />
<label class="checkbox"
for="search-4">Wish to rent</label>
</div>
<!-- input type="submit" class="btn
search-btn" value="Search" / -->
<%= f.submit %>
</div>
<% end %> <!-- form -->控制器( js模板只包含警报):
# GET /items/1
# GET /items/1.json
def list
@selected_location = (params[:loc]) ? params[:loc] : params[:id]
@items = Item.search_items(@selected_location, params[:page])
@modes = Mode.modes_for_search
@super_categories = Supercategory.find(:all)
@locations = Location.locations
@subcategories = Subcategory.where(:category_id => params[:cat]).order("name")
@has_price_info = true # look up based on category id
@show_images=true # look up in params or in user session
respond_to do |format|
format.html # list.html.erb
format.json { render json: @items }
format.js
end
end 发布于 2012-07-02 19:12:46
我找到了解决问题的方法,请参阅“编辑”部分。
https://stackoverflow.com/questions/11285481
复制相似问题