我正在编写一个需要级联选择标记的简单网站。第一个选择标记floor_id中的选定选项会更改第二个选择标记space_id中的选项。我把这2个选项分成两个分开的形式。问题是,select floor_id中的onchange事件不会触发json更改第二个选择标记中的选项,但是提交按钮会执行。一些大师可以告诉我如何解决它吗?万分感谢!
这是app / controllers / spaces_controller.rb中的代码
class SpacesController < ApplicationController
def index
@floors = Floor.all.order(:name)
@spaces = Space.all.order(:name)
end
def list
@floor_id = params[:floor_id]
@spaces = Space.includes(:maps).where( \
"maps.floor_id = ? OR 0 = char_length(?)", \
@floor_id.to_i, \
@floor_id.to_s, \
).references(:map)
render :partial => 'list', :object => @spaces
end
...
end
文件app / assets / javascripts / spaces.js.coffee
$(document).ready ->
$("#category").on("ajax:success", (e, data, status, xhr) ->
$("#space_list").html xhr.responseText
).on "ajax:error", (e, xhr, status, error) ->
$("#space_list").html "<option value=''>Error</option>"
文件app / assets / javascripts / defaults.js
function go_to_uri
( iObject
, iId
, iAction
)
{
if (!iId || iId.length === 0 || iId === "" || typeof iId == 'undefined' || !/[^\s]/.test(iId) || /^\s*$/.test(iId) || iId.replace(/\s/g,"") == "")
return false;
this.document.location.href = iObject + "/" + iId + "/" + iAction;
return false;
}
文件app / views / spaces / index.html.erb
<h1>Spaces</h1>
<%= form_tag list_spaces_url, method: :post, remote: true, id: 'category' do |f| %>
<p>
<%= label_tag :floor_id, 'Floor' %>
<%= select_tag \
:floor_id, \
options_from_collection_for_select(@floors, :id, :name), \
include_blank: true, \
onchange: '$(this).parent(\'form\').submit();' %>
</p>
<%= submit_tag %>
<% end %>
<%= form_tag 'nil', method: :get do |f| %>
<%= label_tag :space, 'Space' %>
<span id="space_list"><%= render 'list' %></span>
<%= button_tag type: \
'button', \
onclick: 'go_to_uri("spaces", this.form.space.value, "map")' \
do %>
Show Map
<% end %>
<% end %>
file app / views / spaces / list.html.erb
<%= select_tag \
:space_id, \
options_from_collection_for_select(@spaces, :id, :name), \
include_blank: true, \
onchange: 'go_to_uri("spaces", this.value, "map")' %>
答案 0 :(得分:0)
我用咖啡脚本重写了onchange事件,现在它正在运行。
文件app / assets / javascripts / spaces.js.coffee
$(document).ready ->
$("#space_category").on("ajax:success", (e, data, status, xhr) ->
$("#space_list").html xhr.responseText
).on "ajax:error", (e, xhr, status, error) ->
$("#space_list").html "<option value=''>Error</option>"
$(".space_category").change ->
$("#space_category").submit()
文件app / views / spaces / index.html.erb
<h1>Spaces</h1>
<%= form_tag list_spaces_url, method: :post, remote: true, id: 'space_category' do |f| %>
<p>
<%= label_tag :floor_id, 'Floor' %>
<%= select_tag \
:floor_id, \
options_from_collection_for_select(@floors, :id, :name), \
include_blank: true, \
class: 'space_catefory' %>
</p>
<%= submit_tag %>
<% end %>
<%= form_tag 'nil', method: :get do |f| %>
<%= label_tag :space, 'Space' %>
<span id="space_list"><%= render 'list' %></span>
<%= button_tag type: \
'button', \
onclick: 'go_to_uri("spaces", this.form.space.value, "map")' \
do %>
Show Map
<% end %>
<% end %>