[英]How to get selected values for dynamic checkbox in mvc?
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 我已经在mvc中创建了一个复选框列表,现在我需要使用jquery来检查复选框值以进行进一步的编码。但是我在使用复选框值时遇到问题,我的代码如下
@{
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 告诉我如何使用jQuery检索复选框值
Thanks in advance 提前致谢
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 然后遍历数组并执行您想执行的任何操作
Try this jquery function, 试试这个jQuery函数,
$(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>
Dont know why you are using normal html tag when u have html helpers provided by razor 当您有razor提供的html帮助器时,不知道您为什么使用普通的html标签
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 checkedValues
将包含选中的复选框的所有值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.