2016-07-27 48 views
0

在我的Rails應用程序我有兩個「button_to」標籤(所以他們的形式)如何輸入複選框附加到形式(button_to)

= button_to("Validate", validate_contracts_path , {method: :patch, class: "bulk-selection"}) 
= button_to("Sign", sign_contracts_path , {method: :patch, class: "bulk-selection"}) 

和我在一個表中有如下一個複選框集,每個複選框代表一行,他們將執行批量操作,並且複選框不包含在表單中。

td = check_box_tag "selected_records[#{record.id}]" 

我想知道是否有可能附加複選框,當其中一個按鈕被點擊。

的多個軌道的方式更好,但它會是巨大的,如果它與JS

實現

感謝

碼詳情: 我不知道還有什麼可以添加到Rails代碼。在這裏,你是在HTML:

按鈕

<form class="button_to" method="post" action="/contracts/sign"> 
    <input type="hidden" name="_method" value="patch"> 
    <input class="bulk-selection" type="submit" value="Firm"> 
    <input type="hidden" name="authenticity_token" value="rTxalZoJ7H4VgSsa4dlrpCe+/uXitZDRhFo6F/qIZbgIoHTh42Qb2gy9dvgDmtKqT2L1x0oYx6EzPXBNKB0KBA=="> 
</form> 

細胞的表

<td><input type="checkbox" name="selected_records[1]" id="selected_records_1" value="1"></td> 
<td><input type="checkbox" name="selected_records[2]" id="selected_records_2" value="2"></td> 
... 
<td><input type="checkbox" name="selected_records[10]" id="selected_records_10" value="10"></td> 
+0

您是否需要提供更多的代碼細節? – hgsongra

+0

完成!告訴我如果你需要別的東西!感謝您的時間 – Quin

+0

因此,複選框不在表單中,您需要在jQuery中獲取所有選中的複選框並將其附加到表單中。 'chekedvalues =「」; ()函數(){(input [name =「test」]:checked')。each(function(){//然後獲取表單對象並添加新的字段形成這個'chekedvalues' – hgsongra

回答

0

這裏是更新snippest您的問題。 添加一個類checkbox稱爲selected_records,並添加JS代碼來獲取選中的複選框值,而且在你的form稱爲selected_records具有相同id添加一個hidden場,獲取所有選中的衣被合計將它設置爲selected_records如下之後。

<script> 
    $(document).ready(function(){ 
     $(".bulk-selection").click(function(){ 
     test = $('.selected_records:checked').map(function() {return this.value;}).get().join(','); 
     $("#selected_records").val(test); 
    }); 
}); 
</script> 

    <td><input type="checkbox" class="selected_records" name="selected_records[1]" id="selected_records_1" value="1"></td> 
    <td><input type="checkbox" class="selected_records" name="selected_records[2]" id="selected_records_2" value="2"></td> 
    ... 
    <td><input type="checkbox" class="selected_records" name="selected_records[10]" id="selected_records_10" value="10"></td> 


<form class="button_to" method="post" action="/contracts/sign"> 
    <input type="hidden" name="_method" value="patch"> 
    <input class="bulk-selection" type="submit" value="Firm"> 
    <input type="hidden" name="authenticity_token" value="rTxalZoJ7H4VgSsa4dlrpCe+/uXitZDRhFo6F/qIZbgIoHTh42Qb2gy9dvgDmtKqT2L1x0oYx6EzPXBNKB0KBA=="> 
    <!-- New Hidden field --> 
    <input type="hidden" name="selected_records" id="selected_records"> 
</form> 

希望這會對你有幫助。