[英]Auto disable other checkboxes using html.checkboxfor once one is selected
我試圖在選擇一個復選框時禁用其他復選框,如果再次取消選中,則再次啟用。
下面是我發現的代碼片段,它在標准復選框的示例小提琴中起作用,但我無法復制此復選框,有人可以推薦我需要進行的更改,以便在復選框中工作?
<script>
$('#chkItems').change(function () {
$(this).siblings('#chkItems').prop('disabled', this.checked)
});
</script>
以下是我的復選框,我稱之為
@Html.CheckBoxFor(modelItem => item.IsDisplayImage, new { id = "chkItems" })
如果想要使用復選框保留,則可以使用此代碼,否則最好使用單選按鈕
HTML
<label class="checkbox-inline check">
<input type="checkbox" name="skills" id="radio" value="1"> 1
</label>
<label class="checkbox-inline check">
<input type="checkbox" name="skills" value="2"> 2
</label>
<label class="checkbox-inline check">
<input type="checkbox" name="skills" value="3"> 3
</label>
JS:
$('.check input:checkbox').click(function() {
$('.check input:checkbox').not(this).prop('checked', false);
});
檢查這個jsfiddle以獲得演示
您可以使用單選按鈕:
<form>
<input type="radio" name="gender" value="male" checked> Male<br>
<input type="radio" name="gender" value="female"> Female<br>
<input type="radio" name="gender" value="other"> Other
</form>
好。 例如,你有模型ForRadioButtonsModel
類的
public class ForRadioButtonsModel
{
public int Id { get; set; }
public bool IsDisplayImage { get; set; }
}
在視圖上您傳遞包含此集合的此模型或模型的集合
return View((List<ForRadioButtonsModel>) collection);
比在視圖中你可以做下一步創建RadioGroup
@foreach (var item in Model) <-- Model is of type List<ForRadioButtonsModel>
{
@Html.RadioButtonFor(m => item.IsDisplayImage, item.IsDisplayImage)
}
這將呈現下一個HTML
<input checked="checked" id="item_IsDisplayImage" name="item.IsDisplayImage" type="radio" value="True">
<input checked="checked" id="item_IsDisplayImage" name="item.IsDisplayImage" type="radio" value="False">
因此,您將使用具有相同名稱但具有不同值的radioButtons
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.