0
我正在寫一個簡單的網站,需要級聯選擇標記。第一個選擇標籤中的選定選項floor_id更改第二個選擇標籤space_id中的選項。我已經把這兩個選項分成了兩個分開的形式。問題是,select floor_id中的onchange事件不會觸發json更改第二個select標籤中的選項,但提交按鈕會執行此操作。有些大師可以告訴我如何解決它嗎?萬分感謝!Rails 4,選擇標記onchange事件無法觸發json on form_tag
這是我在app /控制器/ 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 /資產/ Java腳本/ 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 /資產/ Java腳本/默認.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 /視圖/空間/ 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 %>
文件app /視圖/空間/ 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")' %>