如果我單擊了一個 I..e 動態創建的復選框,如何禁用其他復選框input type=checkbox
@if (ViewBag.Products != null)
{
foreach (var item in ViewBag.Products)
{
<label class="PillList-item">
<input id="Check" type="CheckBox" name="@item.ProductID"/>
<span class="PillList-label" >
@item.Products
<span class="Icon Icon--checkLight Icon--smallest"><i class="fa fa-check"></i></span>
</span>
</label>
}
}
uj5u.com熱心網友回復:
嘗試使用以下代碼:
@section Scripts{
<script>
$(document).ready(function () {
$('input:checkbox').click(function () {
$('input:checkbox').not(this).prop('checked', false);
});
});
</script>
}
uj5u.com熱心網友回復:
首先更新動態復選框代碼,添加“chkInfo”作為類值,即
foreach (var item in ViewBag.Products)
{
<label class="PillList-item">
<input id="Check" type="CheckBox" name="@item.ProductID" class="chkInfo" onclick="Javascript:{UncheckAll_js(this)}"/>
<span class="PillList-label" >
@item.Products
<span class="Icon Icon--checkLight Icon--smallest"><i class="fa fa-check"></i></span>
</span>
</label>
}
然后使用下面的Java Script取消選中所有不需要的復選框,即
// Global variable to access jQuery method from JavaScript method.
var uncheckAll_jq;
// jQuery method goes here.
$(document).ready(function()
{
// This method will disable all the check boxes except the one which is checked
uncheckAll_jq = function UncheckAll(currentCheckBox)
{
// First unchecheck all
$(".chkInfo").removeAttr('checked');
// Then check your target checkbox as already checked.
$(currentCheckBox).attr("checked", true);
}
});
// JavaScript codes goes here.
function UncheckAll_js(currentCheckBox_info)
{
// Invoke jQuery Method.
UncheckAll_jq(currentCheckBox_info);
}
希望這可以幫助!!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/459893.html
標籤:javascript html jQuery asp.net-mvc asp.net 核心
上一篇:id評論系統
