下面的代码在首次运行控制很好,尽管是完全相同的第二控制失败:JavaScript的 - 无法获取的未定义或空引用属性“值”
if (document.getElementById("employeeID").value == "") {
document.getElementById("lblEmp").textContent = "Employee Name is required";
ret = false;
} else {
document.getElementById("lblEmp").textContent = "";
}
if (document.getElementById("grossSalary").value == "") {
document.getElementById("lblGrossSalary").textContent = "Gross Salary is required";
ret = false;
} else {
document.getElementById('#lblGrossSalary').textContent = "";
}
代码正在按预期employeeID
控制,但抛出主题错误的控制grossSalary
。
剃刀:
<div class="form-group">
@Html.LabelFor(model => model.employeeID, htmlAttributes: new { @class = "control-label col-md-2" })
<div id="inner" class="col-md-10">
@Html.EditorFor(model => model.employeeID, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.employeeID, "", new { @class = "text-danger" })
<label id="lblEmp" style="color:red"></label>
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.grossSalary, htmlAttributes: new { @class = "control-label col-md-2" })
<div id="inner" class="col-md-10">
@Html.EditorFor(model => model.grossSalary, new { htmlAttributes = new { @class = "form-control"} })
<label id="lblGrossSalary" style="color:red"></label>
</div>
</div>
什么是@ Html.LabelFor?这看起来不仅仅是JavaScript和HTML。 – mikemaccana
另外'document.getElementById(“grossSalary”)。value'没有称为'grossSalary'的ID,ID的名字是'lblGrossSalary' – mikemaccana