0

I have created a checkbox list in mvc and now i need to take the checked checkbox values using jquery for further coding.But i'm having problem in taking the checkbox values and my code is below

 @{
int i = 1;
foreach (CheckBoxList item in Model.Items)
{
     <input type="checkbox" name="#= Text #" id="#= Text#(i) #" value="#= item.Value #" />
     <span> @item.Text</span>
  <br/>
   i++;
}
}

tell me how to retrieve checkbox values using jquery

Thanks in advance

3 Answers 3

1

Simply create an array of object and push the values

var allCheckboxInfo=[];

$('input[type="checkbox"]').each(function(){

var checkBoxItemInfo=new {};
checkBoxItemInfo.id=this.id;
checkBoxItemInfo.value=this.value;

allCheckboxInfo.push(checkBoxItemInfo);


});

Then loop through array and do what ever you want to perform

Sign up to request clarification or add additional context in comments.

Comments

0

Try this jquery function,

$(function(){
  $('#save_value').click(function(){
    var val = [];
    $(':checkbox:checked').each(function(i){
      val[i] = $(this).val();
    });
  });
});

or may be try this,

<script type="text/javascript">
$(document).ready(function () {
  var selectedValues="";
  $checkedCheckboxes = $("input:checkbox:checked");
    $checkedCheckboxes.each(function () {
      selectedValues +=  $(this).val() +",";
    });
 alert(selectedValues);//u get a comma separated list
});
</script>

Comments

0

Dont know why you are using normal html tag when u have html helpers provided by razor

cant u use

  @Html.CheckBoxFor(model=>model.chkVal)

Try this

 var checkedValues = $('input[type=checkbox]:checked').map(function()
                {
                    return $(this).val();
                }).get();

checkedValues will contain all values for checked chekboxes

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.