簡體   English   中英

MVC C# 中的多個復選框 select

[英]Multiple checkbox select in MVC C#

我有如下所示的表格。 我想根據 I select 類型檢查/取消檢查(現在只是嘗試檢查)表中的值。可以這樣完成嗎? 或者我是否需要以某種方式從 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:

function selectType(items, type)
{
   return items.filter(x => x.ObjectType === type).map(x => x.Checked = true);
}

但是我需要在復選框輸入中添加checked屬性,但我不知道該怎么做

根據此文檔,我可以看到您可以使用以下方式切換“選中的屬性”:

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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM