在JavaScript中使用RubyonRails标记
我在html.erb文件中有一个html按钮。此按钮在表中生成html控件和附件。在没有RubyonRails脚本的情况下,它工作得很好。但现在我已经选择了RoR的标签。这是我的页面。我在评论中询问的完整代码如下:在JavaScript中使用RubyonRails标记,javascript,jquery,ruby-on-rails,Javascript,Jquery,Ruby On Rails,我在html.erb文件中有一个html按钮。此按钮在表中生成html控件和附件。在没有RubyonRails脚本的情况下,它工作得很好。但现在我已经选择了RoR的标签。这是我的页面。我在评论中询问的完整代码如下: <script type="text/javascript"> $(document).ready(function() { $("#AddSch").click(function() { var hidval
<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会将其更改为textbox,如果我使用type=button,它就不会调用该javascript。我怎样才能让它工作呢?我可以给你两个指针,我无法真正复制,因为我没有你的模型和控制器动作设置 一,。您不希望随机增加或减少变量的大小写,如:
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"
理想情况下,你不会想要内联样式太多,但我们现在就让它溜走-
二,。如果变量input1未定义,请查看您的devtools控制台F12,并检查是否存在任何JS错误
希望有帮助,
Jan您的javascript文件的完整路径/名称是什么?您如何确保javascript文件中有Rails的助手等,并且可以执行ERB?单击type=button输入时执行一些javascript的基本思想很好-check out@joshua.paling我在放置add按钮的同一页面中使用javascript,即new.html.ERB。我已经提到,这个添加按钮和点击事件工作正常。但当我将此rails帮助程序放置在一个代码块中,用于选择Update your question以包含完整的代码供查看时,它会提交页面。@joshua.paling我已经发布了完整的代码。并删除不相关的片段。请查收。谢谢,我删除了var input1部分,忘记从我发布的代码中删除它。我所遵循的内容与在f.select和select_标签中使用的内容相同,所以这也不是问题。无论如何,谢谢你的关注