JQuery ERB 模板不插入 HTML
我是 JQuery/JS 的初学者,正在尝试在 Rails 3.1 应用程序中实现依赖下拉列表。我有以下形式:
<%= debug params %>
<%= form_for([@wall.climbing_centre, @wall]) do |f| %>
<% if @wall.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@wall.errors.count, "error") %> prohibited this wall from being saved:</h2>
<ul>
<% @wall.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div id="kind" class="field">
<%= f.label :kind %><br />
<%= f.select :kind, Wall::Kinds, :input_html => {:rel => "/kinds"} %>
</div>
<div class="field">
<%= f.label :wall_number %><br />
<%= f.text_field :number%>
</div>
<div id="gradelist" class="field">
<%= f.label :grade %><br />
<%= f.select :grade, Wall::BGrades %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
以下 JavaScript
jQuery.ajaxSetup({
'beforeSend': function(xhr) { xhr.setRequestHeader("Accept", "text/javascript") }
});
jQuery(function($) {
// when the #kind field changes
$("#kind").change(function() {
// make a POST call and replace the content
$.post('/kinds', {id: $("#wall_kind").val()}, null, "script");
return false;
});
})
以下 JS.ERB 模板:
$("#gradelist").html("<%= label :gbabe, 'Grade'%></br><%=select_tag :gbabe, options_for_select(@grades) %>");
我的墙壁控制器中的以下操作: def Grades_by_kind
if params[:id].present?
@grades = (params[:id] == "Boulder" ? Wall::BGrades : Wall::FGrades)
else
@grades = []
end
respond_to do |format|
format.js
end
end
我在墙模型中有以下常量:
FGrades = %w[5 5+ 6A 6A+ 6B 6B+ 6C 6C+ 7A 7A+ 7B 7B+ 8A 8A+ 8B 8B+ 9A 9A+]
BGrades = %w[V0 V0+ V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 15]
Kinds = ['Boulder', 'Leading', 'Top Rope']
更改 :kind 选择框中的选择,触发 JS post 请求,并收到以下响应:
$("#wall_grade").html("<label for="gbabe_Grade">Grade</label></br><select id="gbabe" name="gbabe"><option value="5">5</option>
<option value="5+">5+</option>
<option value="6A">6A</option>
etc...
<option value="8B+">8B+</option>
<option value="9A">9A</option>
<option value="9A+">9A+</option></select>");
但是, id="gradelist" div 中的下拉值显示在网页实际上并没有改变。为什么 HTML 没有被改变?
I'm a beginner at JQuery/JS and am trying to implement a dependent drop-down list in a Rails 3.1 app. I have the following form:
<%= debug params %>
<%= form_for([@wall.climbing_centre, @wall]) do |f| %>
<% if @wall.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@wall.errors.count, "error") %> prohibited this wall from being saved:</h2>
<ul>
<% @wall.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div id="kind" class="field">
<%= f.label :kind %><br />
<%= f.select :kind, Wall::Kinds, :input_html => {:rel => "/kinds"} %>
</div>
<div class="field">
<%= f.label :wall_number %><br />
<%= f.text_field :number%>
</div>
<div id="gradelist" class="field">
<%= f.label :grade %><br />
<%= f.select :grade, Wall::BGrades %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
The Following JavaScript
jQuery.ajaxSetup({
'beforeSend': function(xhr) { xhr.setRequestHeader("Accept", "text/javascript") }
});
jQuery(function($) {
// when the #kind field changes
$("#kind").change(function() {
// make a POST call and replace the content
$.post('/kinds', {id: $("#wall_kind").val()}, null, "script");
return false;
});
})
The following JS.ERB template:
$("#gradelist").html("<%= label :gbabe, 'Grade'%></br><%=select_tag :gbabe, options_for_select(@grades) %>");
The following action in my walls controller:
def grades_by_kind
if params[:id].present?
@grades = (params[:id] == "Boulder" ? Wall::BGrades : Wall::FGrades)
else
@grades = []
end
respond_to do |format|
format.js
end
end
I have the following constants in the wall model:
FGrades = %w[5 5+ 6A 6A+ 6B 6B+ 6C 6C+ 7A 7A+ 7B 7B+ 8A 8A+ 8B 8B+ 9A 9A+]
BGrades = %w[V0 V0+ V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 15]
Kinds = ['Boulder', 'Leading', 'Top Rope']
Changing a selection in the :kind select box, triggers the JS post request, and receives the following response:
$("#wall_grade").html("<label for="gbabe_Grade">Grade</label></br><select id="gbabe" name="gbabe"><option value="5">5</option>
<option value="5+">5+</option>
<option value="6A">6A</option>
etc...
<option value="8B+">8B+</option>
<option value="9A">9A</option>
<option value="9A+">9A+</option></select>");
However, the drop down values in the id="gradelist" div presented in the webpage aren't actually changing. Why isn't the HTML being changed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的选择不会改变,因为您没有对从服务器获得的响应执行任何操作。您需要在
$.post
调用中指定 回调。另外,您需要在返回成绩之前将成绩转换为 html 选项列表,可能使用options_for_select
Your select isn't changing because you're not doing anything with the response you're getting from the server. You need to specify a callback in your
$.post
call. Also, you need to convert your grades to an html option list before returning them, perhaps withoptions_for_select