I have checkboxes in a loop.
for example..
<% (1..5).each do |chk| %>
<%= check_box_tag 'chk',"#{chk}", :checked=>false %>
<% end %>
I want to get the check box id's which are all checked.
Please help me to do that.
Thanks in advance.
I have checkboxes in a loop.
for example..
<% (1..5).each do |chk| %>
<%= check_box_tag 'chk',"#{chk}", :checked=>false %>
<% end %>
I want to get the check box id's which are all checked.
Please help me to do that.
Thanks in advance.
Jquery Link :
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
Try this:
HTML Code :
<input id="chk_1" name="foo" type="checkbox" value="1" />chk1
<input id="chk_2" name="foo" type="checkbox" value="2" />chk2
<input id="chk_3" name="foo" type="checkbox" value="3" />ckh3
<input id="chk_4" name="foo" type="checkbox" value="4" />chk4
<input id="chk_5" name="foo" type="checkbox" value="5" />chk5
<button>Alert List</button>
JsCode :
(function( $ ){
$.fn.valList = function(){
return $.map( this, function (elem) {
return elem.value || "";
}).join( "," );
};
$.fn.idList = function(){
return $.map( this, function (elem) {
return elem.id || "";
}).join( "," );
};
})( jQuery );
$('button').click(function(){
alert($("input:checked").valList());
alert($("input:checked").idList());
});
Running Example : JsFiddle Link
$("input[type='checkbox']").val(); will only return the value of the first checked box.Try with:
<% (1..5).each do |chk| %>
<%= check_box_tag 'chk[]',"#{chk}", :checked=>false%>
<% end %>
Then, in the controller, params[:chk] will give you an array of check box values which are checked.
Hope it helps :)
Try This It may helps you
Ruby syntax:
<% (1..5).each do |chk| %>
<%= check_box_tag('chk[]', chk) %>
<%= chk %>
<% end %>
Jquery Syntax:
$('document').ready(function(){
$('input["name=chk[]"]').change(function(){
if($(this).attr("checked")){
alert($(this).val())}
})
})
which gives alert of the checked check box value when you change checkbox.
if you want checked check box value when document loads then try this
$('document').ready(function(){
alert($('input["name=chk[]"]:checked').val())
})