从 jquery 和 haml 渲染部分内容
我计划执行的功能是根据从选择标记中选择的数字插入一些表单元素。
我有一个名为 number_of_passengers 的选择标签,并且我计划为所选数量动态附加新的乘客字段。假设我从 number_of_passengers 中选择 2 个表单,那么 2 个表单应该出现在字段集中。这些表格包含姓名、年龄、体重等。
我尝试按照以下步骤操作:
并将其转换为 haml-speak,但每当我使用 :javascript 标记时都会出现错误。另外,我认为一旦我进入其中,我就无法“转义”javascript标签
:javascript
$('#number_of_passengers').change(function() {
var $num_of_passengers = $(this).val();
for($i=0; $i<$num_of_passengers;$i++) {
$('.passenger-info ul').append('<%= escape_javascript( render :partial => "new_passenger", :locals => {:booking => @booking }) %>');
}
})
,因为我处于form_for中,如何将@booking变量传递到本地?这看起来真的很复杂,我计划用肮脏的方式循环 20 次(最多 20 名乘客),然后根据所选的数字隐藏/显示它们。但你不觉得这太脏了吗?
The functionality I plan on doing is to insert some form elements depending on a number chosen from a select tag.
I have a select tag called for number_of_passengers, and i plan to dynamically append new passenger fields for the number chosen. Say I select 2 from number_of_passengers, then 2 forms should appear in a fieldset. these forms contain name, age weight etc.
I tried following this:
call a rails function from jquery?
and just converted it to haml-speak but I get errors whenever I use the :javascript tag. Also I don't think I can "escape" the javascript tag once I am in it
:javascript
$('#number_of_passengers').change(function() {
var $num_of_passengers = $(this).val();
for($i=0; $i<$num_of_passengers;$i++) {
$('.passenger-info ul').append('<%= escape_javascript( render :partial => "new_passenger", :locals => {:booking => @booking }) %>');
}
})
also since I am in a form_for, how do I pass the @booking variable to the local? It seems really complicated and I'm planning of doing the dirty way out of just looping 20 times(20 max passengers) then just hide/show them depending on the selected number. But that's too dirty don't you think?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要使插值工作,您需要执行
更简单的操作:将
!=
添加到行的开头,并将字符串包含在双引号之间。您关于
@booking
的问题:我建议您研究一些嵌套表单示例,以便让您更清楚(例如 这个 和To get the interpolation working you need to do something like
so simpler said: add the
!=
to the start of the line, and include the string between double quotes.Your question about the
@booking
: I suggest you investigate some nested forms examples to make it clearer for you (e.g. this and this railscast).我实际上通过添加来做到这一点:
所以即使请求是在js中,它也会返回一个html部分。俏皮
i actually managed to do this by adding:
so even if the request was in js, it will return an html partial. Nifty
Rails 语法一直在变化,但这不应该是这样。
您
可以在 javascript 块中转义 javascript,因为您正在评估 ruby 标签。
您收到哪些错误消息?
我不清楚你问题的后半部分,但这就是你要找的:
Rails syntax changes all the time but shouldn't this
be
You can escape javascript within the javascript block because you're evaluating ruby tags.
Which error messages are you receiving?
The latter part of your question isn't clear to me but is this what you're looking for: