[英]Using Ruby on Rails tag in JavaScript
我在html.erb
文件中有一个html按钮。 此按钮生成html控件并追加到表中。 当不涉及rails脚本的红宝石时,它工作正常。 但是现在我有了RoR的select_tag
。 这提交了我的页面。 我在评论中要求的完整代码如下:
<script type="text/javascript">
$(document).ready(function() {
$("#AddSch").click(function() {
var hidval = $('#valEdu').val();
if(hidval == 0)
{
hidval = 1;
}
else
{
//$('#valEdu').val(++hidval);
hidval++;
}
var rownum=$("#Controls > tbody > tr").length;
var updated_row_num;
if (rownum == 0)
{
updated_row_num=0;
}
else {
updated_row_num=rownum/2;
}
var newRow = "<tr><td align='center' style='font-size: x-large; color: #212121;' height='35px'>" + input1 + " <%= select_tag('university', options_from_collection_for_select(@Universites, 'id', 'University'),{:prompt => 'University'}) %></td></tr>";
var control = "<tr><td align='center'><button type='button' class='btn_rmv'>Remove</button></td></tr>";
$('#Controls').append(newRow);
$('#Controls').append(control);
return false;
});
$('#Controls').on('click', '.btn_rmv', function() {
var index = $(this).closest('tr').index() + 2
$('#Controls tr:nth-child(n+' + (index - 3) + ')').remove();
return false;
});
});
</script>
<h2 class = "subtitle">
Education
</h2>
<%= form_for (:Educations) do |f| %>
<table width="100%">
<tr style = "text-align:center;">
<td>
<%= f.select :chapter,options_from_collection_for_select(@Chapters, "id", "Chapter"), :include_blank => "Chapter",:id => "DDL_Students",:style => "margin-top: 10px" %>
<%= f.select :University,options_from_collection_for_select(@Universites, "id", "University"), :include_blank => "University",:id => "DDL_Students",:style => "margin-top: 10px" %>
</td>
</tr>
<tr>
<td align="center">
<table id="Controls">
</table>
<div><input id="AddSch" value="Add" type="button" /></div>
<input id="valEdu" type="hidden" value="0" />
</td>
</tr>
</table>
<table width="92%">
<tr>
<td align="center">
<div class="button" style="margin-left:60px;">
<%= f.submit "Next", { :class => "buttonSearch"} %>
</div>
</td>
</tr>
</table>
<% end %>
删除type="submit"
会将其更改为文本框,如果我使用type="button"
它将永远不会调用该javascript。 我该如何运作?
我可以为您提供两个指示,因为您没有模型和控制器操作设置,因此我无法真正复制。
1.您不想像下面那样随机增大或减小大小写:
f.select :University,options_from_collection_for_select(@Universites, "id", "University"), :include_blank => "University",:id => "DDL_Students",:style => "margin-top: 10px"
相反,您想要:
f.select :university, options_from_collection_for_select(@universities, "id", "university_name"), :include_blank => "University",:id => "DDL_Students",:style => "margin-top: 10px"
理想情况下,您也不想使用内联样式,但是现在让我们放心吧;-)
2.我可能未定义变量“ input1”,请查看您的devtools控制台(F12),并检查那里是否存在任何JS错误。
希望能有所帮助,扬
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.