我有如下所示的表格。我想根據我選擇的型別檢查/取消選中(現在只是嘗試檢查)表中的值。這可以以某種方式完成嗎?或者我是否需要從 javascript 發送請求以查看更新的值?或者這可以通過僅使用 C# 來完成嗎?
var list = JsonConvert.SerializeObject(@table);
<form asp-controller="Consumer" asp-action="" method="post">
<div class="row justify-content-end" style="margin-top: 20px; margin-bottom: 20px;">
@foreach (var type in ObjectTypes.All)
{
<label style="margin-right: 10px;">
<input onclick="selectType(@list, @type)" style="margin-right: 5px;" type="checkbox" id="@type" name="type" value="@type">@type
</label>
}
</div>
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th></th>
<th>Object Id</th>
<th>Object type</th>
</tr>
</thead>
<tbody>
@if (table.Count > 0)
{
@for (int i = 0; i < table.Count; i )
{
<input type="hidden" name="@("items[" i "].ObjectId")" value="@table[i].ObjectId"/>
<input type="hidden" name="@("items[" i "].ObjectType")" value="@table[i].ObjectType"/>
<tr>
<td>@( id)</td>
<td>
<input name="@("items[" i "].Checked")" type="checkbox" value="true" @(table[i].Checked ? "checked" : " ")/>
</td>
<td>@table[i].ObjectId</td>
<td>@table[i].ObjectType</td>
</tr>
}
}
</tbody>
</table>
</form>
我有這樣的js功能:
function selectType(items, type)
{
return items.filter(x => x.ObjectType === type).map(x => x.Checked = true);
}
但我需要checked在復選框輸入中添加屬性,我不知道應該怎么做
uj5u.com熱心網友回復:
根據此檔案,我可以看到您可以使用以下方法切換“已檢查屬性”:
document.getElementById("myCheck").checked = true;
但我不明白你為什么要在你的selectType方法中回傳一些東西。它是當您單擊復選框時呼叫的方法,因此您不需要回傳任何內容。您只需要更改此標簽的 Checked 屬性。像這樣的東西應該可以解決問題:
function selectType(items, type)
{
document.getElementById(type).checked = true; // I assume type is the Id of the element
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/458730.html
標籤:javascript C# 。网 asp.net-mvc 剃刀
上一篇:當我將引數傳遞給“WebClient.DownloadFile”時會引發錯誤,但如果我對值進行硬編碼,則可以正常作業
下一篇:檢查元素的總和
