下面的代碼在首次運行控制很好,儘管是完全相同的第二控制失敗: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