[英]One checkbox to disable some CheckBoxFor
I'm working on a MVC program. 我正在开发MVC程序。
I'm using Razor language in a View to display a list of Projects (from a Database through Controller/Model) with CheckBoxFor : 我在View中使用Razor语言来显示带有CheckBoxFor的项目列表(从数据库通过Controller / Model):
for (int i = 0; i < Model.ProjectList.Count; i++)
{
@Html.HiddenFor(m => m.ProjectList[i].Id)
<li>@Html.CheckBoxFor(m => m.ProjectList[i].IsChecked,
new { id = "projectList_" + i, className = "CheckBoxProject" })
@Model.ProjectList[i].Name</li>
}
This part works fine. 这部分工作正常。
I would like to add another CheckBox to uncheck and disable the previous ChekBoxFor and to prevent them to be checked. 我想添加另一个CheckBox来取消选中并禁用以前的ChekBoxFor,并阻止对其进行检查。 I tried to do this with some JavaScript put at the end of the View: 我试图通过在视图末尾放置一些JavaScript来做到这一点:
<input type="checkbox" id="SetBox" value="SetBox" onclick="CheckBoxState(this);">
<script type="text/javascript" onload="loaded=1">
function CheckBoxState(elementRef) {
document.querySelectorAll('.CheckBoxProject').checked = false;
document.querySelectorAll('.CheckBoxProject').disabled = true;
};
So if I understand what I have done, the "onclick" should call the Javascript function called "CheckBoxState" . 因此,如果我了解自己已完成的工作,则“ onclick”应调用名为“ CheckBoxState”的Javascript函数。 Then grab all the CheckBoxFor having the className CheckBoxProject , uncheck and disable them. 然后获取所有具有className CheckBoxProject的CheckBoxFor ,取消选中并禁用它们。
When I'm trying it in Firefox, nothing happens when I check the last CheckBox and the page inspector shows a warning something like (is not in english) : failed to load script where the source is " http://localhost:..../browserLink " 当我在Firefox中尝试时,当我检查最后一个CheckBox并且页面检查器显示类似(不是英语)的警告时,没有任何反应: 无法加载源为“ http:// localhost :. ”的脚本。 ../browserLink “
I'm not sure how to tell to the View that i'm using Javascript and if Razor language is able to works with Javascript . 我不确定如何告诉View我正在使用Javascript,以及Razor语言是否可以使用Javascript 。
I tried different ways to code the JavaScript depending of what i found on StackOverflow and after several failure i resign myself to ask... 我尝试了不同的方式来编写JavaScript代码,具体取决于我在StackOverflow上找到的内容,并且几次失败后我都辞职了……
Finnally i found something that works with jQuery (that i didn't know) : 最后,我发现了一些适用于jQuery的东西(我不知道):
<input type="checkbox" id="SetBox"> <label>Uncheck and disable other Checkbox</label>
<script type="text/javascript">
// Call function when the element with the id = SetBox is clicked
$('#SetBox').click(function(){
var SetBox = document.getElementById("SetBox");
if (SetBox.checked)
{
// select all checkbox (even SetBox) and unchecked + disabled them
$('input:checkbox').prop('checked', false);
$('input:checkbox').prop('disabled', true);
// select only SetBox and check + enable it.
$('#SetBox').prop('checked', true);
$('#SetBox').prop('disabled', false);
}
else
{
// Make all checkbox available if SetBox is unchecked.
$('input:checkbox').prop('disabled', false);
}
});
It works with Html.CheckBoxFor(). 它与Html.CheckBoxFor()一起使用。
I still have the warning : failed to load script where the source is " http://localhost:..../browserLink " But it seems it doesn't matter. 我仍然有警告:无法加载源为“ http:// localhost:.... / browserLink ”的脚本,但似乎没有关系。
I hope this can help someone later. 希望以后可以对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.