简体   繁体   English

JavaScript Rails代码在application.js中不起作用

[英]JavaScript Rails code not working in application.js

  $(document).ready(function() { 
    $('#client-select2').select2();
   });

If i place the code above to application.js, it does not respond but if I place it in the _form.html.erb file, it does. 如果我将上面的代码放置到application.js,它不会响应,但是如果我将它放置在_form.html.erb文件中,它就会响应。 Anyone know why that is? 有人知道为什么吗?

views/orders/_form.html.erb views / orders / _form.html.erb

<script type="text/javascript">
  $(document).ready(function() { 
    $('#client-select2').select2();
   });

</script>

<%= simple_form_for(@order) do |f| %>
  <%= f.error_notification %>
    <%= f.input :code %>
    <%= f.association :client, collection: Client.all, label_method: :name, value_method: :id, prompt: "Choose a Client", required: true, input_html: { id: 'client-select2' } %>
    <%= f.submit %>
<% end %>

application.html.erb has application.html.erb有

<%= javascript_include_tag 'application' %>

The reason could be that select2 and/or jQuery is included after your application.js, 原因可能是在application.js之后包含了select2和/或jQuery,

If for instance, if the order is: 例如,如果订单是:

//=require_self
//=require select2
//=require jquery

The jquery/select2 functions will not be available in application.js since application.js is included before. jquery / select2函数在application.js中将不可用,因为之前已包含application.js。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM