我有一個窗體有很多選擇輸入按主題分組。我爲每個組選擇了一個主選擇,允許用戶將組中的所有選擇設置爲主值(gradescales是提供1-8級的方法)。DRY向上jquery函數
我怎麼能幹這個呢?
FORM
<label>Change all to:</label>
<%= select_tag "section_one", options_for_select(gradescales), :id => "section_one_master" %>
<div id="section_one_wrapper">
<%= f.input :item_one, :collection => gradescales %>
<%= f.input :item_two, :collection => gradescales %>
<%= f.input :item_three, :collection => gradescales %>
<%= f.input :item_four, :collection => gradescales %>
</div>
jQuery函數如下所示:
$("#section_one_master").change(function() {
var a = $("#section_one_master").val();
$("#section_one_wrapper select").val(a);
});
$("#section_two_master").change(function() {
var a = $("#section_two_master").val();
$("#section_two_wrapper select").val(a);
});
...again, and again
所以,當section_one_master改變時,功能被擊中,所有的section_one_wrapper DIV中的選擇輸入更改以匹配任何值被選爲主人。
我有很多組,每個組都有一個主選擇輸入。我怎麼能幹這一個功能,然後動態地標識調用函數的主,並使用約定來改變適當的分組選擇?
使用類而不是id的。 – Blazemonger