【发布时间】:2021-01-08 09:44:08
【问题描述】:
我使用 ViewModel 并且我有带有复选框的表格,我只能逐个检查框,但我需要检查所有选项,但我需要定义我的 var 值,因为我将这些值传递给控制器并做一些事情。
$('.checktip').click(function () {
var idemployee = $('.idemployee').val();
var idtip= $(this).attr('idtip');
var chk = $(this).prop('checked');
$.ajax({
url: UrlSettingsDocument.Books,
data: { idemployee: idemployee, idtip: idtip, chk: chk },
type: "POST",
success: function (result) {
if (result.result == "Redirect") {
window.location = result.url;
}
}
});
});
我的 .cshtml
<table class="table table-striped grid-table">
<tr>
<th>Samp</th>
<th>Id of Book</th>
<th>
//Button for check all *(NOT IMPLEMENTED)*
<button type="button" class="checkall">Select/Deselect</button>
</th>
</tr>
@foreach (var item in (IEnumerable<cit.Models.getCheIdTip_Result>)Model)
{
<tr>
<td>@item.idtip</td>
<td>@item.tipname</td>
<td>
<div class="pure-checkbox">
<input type="checkbox" idtip="@item.idtip" class="checktip" checked="@(item.idemployee == ViewBag.idemployee ? true : false)" name="@item.id.ToString()" id="@item.id.ToString()" />
<label for="@item.id.ToString()"></label>
</div>
</td>
</tr>
}
</table>
<input type="hidden" value="@ViewData["idemployee"]" name="idemployee" id="idemployee" class="idemployee" />
</div>
【问题讨论】:
-
您好,您能否详细说明您的问题。
-
@Swati 我的 js 代码一个一个地检查复选框,当我重新加载它时它被保存了,没关系,但现在我想添加
<button type="button" class="checkall">Select/Deselect</button>来检查它们,当我重新加载它需要都得救了。我使用这个 var 元素var idemployeevar idtipvarvar chk并将它们传递给控制器,并为每个员工执行保存在数据库复选框中。那么如何通过传递相同的 var 元素来做到这一点,但要检查点击功能上的所有框。 -
所以你需要通过所有的复选框
idtip&checkedstatus ? -
@Swati 是的,这就是我需要的。
-
您可以通过
loop到checked值然后将这些值推送到json array尝试让我知道您是否仍然无法正常工作。 This 答案应该会有所帮助。
标签: javascript c# jquery ajax asp.net-mvc