如何使用 jquery 在 ASP.Net MVC 中创建复选框列表
how to create a checkboxlist in ASP.Net MVC using jquery
我想根据下拉菜单的选定值生成动态复选框。
@Html.DropDownListFor(x => x.Fromsource, new SelectList(Model.Fromsource, "Value", "Key"), "---Select---", new
{
})
@Html.CheckBox("Tosource", false, new { @class = "dropdown" })
<script>
$("#Fromsource").change(function () {
var urlSource = '@Url.Action("FillToSource", "PublishPost")';
var fromsouyrcetype = $(this).val();
if (fromsouyrcetype != "") {
$.ajax({
url: urlSource,
method: "GET",
data: {
"sourceType": fromsouyrcetype
},
success: function (result) {
$.each(result, function (index, value) {
//what to write here?
//value.name = name of the checkbox
//value.id = value of the checkbox
});
}
});
} else {
}
});</script>
value.id
和 value.name
是我要为上面评论中提到的复选框填写的值。
如果我对你的问题理解正确,你想根据从服务器获得的结果动态创建复选框(每次下拉值更改时执行查询):
<div id="checkboxContainer"></div>
<script>
$("#Fromsource").change(function () {
var urlSource = '@Url.Action("FillToSource", "PublishPost")';
var fromsouyrcetype = $(this).val();
if (fromsouyrcetype != "") {
$.ajax({
url: urlSource,
method: "GET",
data: {
"sourceType": fromsouyrcetype
},
success: function (result) {
$('#checkboxContainer').empty();
var content;
$.each(result, function (index, value) {
content += '<input type="checkbox" name="'+ value.name +'" id="'+value.id+'"/>'
});
$('#checkboxContainer').html(content);
}
});
} else {
$('#checkboxContainer').empty();
}
});</script>
我想根据下拉菜单的选定值生成动态复选框。
@Html.DropDownListFor(x => x.Fromsource, new SelectList(Model.Fromsource, "Value", "Key"), "---Select---", new
{
})
@Html.CheckBox("Tosource", false, new { @class = "dropdown" })
<script>
$("#Fromsource").change(function () {
var urlSource = '@Url.Action("FillToSource", "PublishPost")';
var fromsouyrcetype = $(this).val();
if (fromsouyrcetype != "") {
$.ajax({
url: urlSource,
method: "GET",
data: {
"sourceType": fromsouyrcetype
},
success: function (result) {
$.each(result, function (index, value) {
//what to write here?
//value.name = name of the checkbox
//value.id = value of the checkbox
});
}
});
} else {
}
});</script>
value.id
和 value.name
是我要为上面评论中提到的复选框填写的值。
如果我对你的问题理解正确,你想根据从服务器获得的结果动态创建复选框(每次下拉值更改时执行查询):
<div id="checkboxContainer"></div>
<script>
$("#Fromsource").change(function () {
var urlSource = '@Url.Action("FillToSource", "PublishPost")';
var fromsouyrcetype = $(this).val();
if (fromsouyrcetype != "") {
$.ajax({
url: urlSource,
method: "GET",
data: {
"sourceType": fromsouyrcetype
},
success: function (result) {
$('#checkboxContainer').empty();
var content;
$.each(result, function (index, value) {
content += '<input type="checkbox" name="'+ value.name +'" id="'+value.id+'"/>'
});
$('#checkboxContainer').html(content);
}
});
} else {
$('#checkboxContainer').empty();
}
});</script>