[英]How to get selected values for dynamic checkbox in mvc?
我已經在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++;
}
}
告訴我如何使用jQuery檢索復選框值
提前致謝
只需創建一個對象數組並推送值
var allCheckboxInfo=[];
$('input[type="checkbox"]').each(function(){
var checkBoxItemInfo=new {};
checkBoxItemInfo.id=this.id;
checkBoxItemInfo.value=this.value;
allCheckboxInfo.push(checkBoxItemInfo);
});
然后遍歷數組並執行您想執行的任何操作
試試這個jQuery函數,
$(function(){
$('#save_value').click(function(){
var val = [];
$(':checkbox:checked').each(function(i){
val[i] = $(this).val();
});
});
});
或者可以嘗試一下
<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>
當您有razor提供的html幫助器時,不知道您為什么使用普通的html標簽
你不能使用
@Html.CheckBoxFor(model=>model.chkVal)
嘗試這個
var checkedValues = $('input[type=checkbox]:checked').map(function()
{
return $(this).val();
}).get();
checkedValues
將包含選中的復選框的所有值
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.