簡體   English   中英

選擇標記jQuery更改檢測在腳本中工作正常,而在帶有CoffeeScript的Rails 4.2.x項目中不工作

[英]Select tag jQuery change detection working fine in script, not working in Rails 4.2.x project with CoffeeScript

我們試圖在Rails 4.2項目的視圖中使用jQuery / CoffeeScript檢測選擇標簽的變化。 在我們的測試JSFiddle中,它工作正常。

但是,在我們的Rails視圖中,該按鈕可以使用,但是select標簽更改檢測不起作用。 我們的個人資料索引視圖中包含以下內容:

<%= form_for current_user.profile, :url => url_for(:controller => 'profile'), :method => :POST do |f| %>
    <div id="inf_profile_settings">
        <table id="tags-table" style="width:100%">
            <tr>
                <th>Name</th>
                <th>Lunches</th>
            </tr>
            <tbody id="tags-table-body" class="tags-table-body">
                <%= f.fields_for :tags do |tag| %>              
                    <%= render "tag_fields", :f => tag %>            
                <% end %>
                <div class="test_tags_button_container">
                    <%= button_tag "Test Tags", :id => "test-tags-button", :class => "test-tags-button", :type => "button", 'data-no-turbolink' => 'true' %>
                </div>
            </tbody>
        </table>
    </div>
<% end %> 

<%= render“ tag_fields”,:f => tag%>代碼與為每個表行加載的_tag_fields部分有關,這允許用戶動態添加行。 這就是_tag_fields部分中的所有內容:

<tr class='nested-fields'>
    <td><%= f.select :inf_acct_id, @inf_names %></td>
    <td><%= f.select :lunches, @lunches1 %></td> 
</tr>

在profile.coffee中,我們具有以下內容:

$(document).ready ->
    $("[id^='profile_attributes_'][id$='_inf_acct_id']").change(->
        alert "TestChange"
    )
    $(document).on 'click', '.test-tags-button', checkTestTagsButton

“ checkTestTagsButton”功能只是一個警報,可以正常工作。 選擇標簽更改檢測也是一個警報,無法正常工作。

我們正在使用的框架會自動生成分配給ID的ID,這些ID會從_tag_fields部分中呈現出來。 如果您在Firebug中檢查它們,它將分配選擇ID,如下所示:

<select id="profile_tags_attributes_0_inf_acct_id" name="profile[tags_attributes][0][inf_acct_id]">

<select id="profile_tags_attributes_1_inf_acct_id" name="profile[tags_attributes][1][inf_acct_id]">

...等等。 如圖所示,框架將基於行從0開始將整數插入選擇ID的中間。這就是為什么我們使用字符串“ [id ^ ='profile_attributes _'] [id $ ='_ inf_acct_id']”的原因來檢測ID是否以“ profile_attributes_”開頭並以“ inf_acct_id”結尾的所有選擇標簽是否已更改,如另一篇文章所述。

在上面發布的JSFiddle中,按鈕和選擇標簽檢測都可以正常工作,而在Rails視圖中,只有按鈕可以工作,原因是我們無法確定。

在這個問題上的任何幫助將不勝感激。

嘗試

$ ->
  for el in $("[id^='profile_attributes_']")
    $(el).on 'change', (e) ->
      console.log e.currentTarget, 'changed'

也嘗試替換input change

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM