JavaScript - 无法获得财产'价值'未定义或空引用的

时间:2016-06-08 11:57:56

标签: 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>

3 个答案:

答案 0 :(得分:2)

应该是

document.getElementById('lblGrossSalary').textContent = "";

而不是

document.getElementById('#lblGrossSalary').textContent = "";

在你的最后一个块中

答案 1 :(得分:1)

看起来问题可能如下:

else {
     document.getElementById('#lblGrossSalary').textContent = "";
 }

('#lblGrossSalary')更改为('lblGrossSalary')

答案 2 :(得分:0)

你只能在对象上使用点(。)运算符,所以如果它是undefined或null并且想要&#34; .value&#34;你会收到一个错误。

我重新编写了你的​​代码,但这应该适合你!它也更清洁:)

Error: unable to load the file libsbmlj.jar.  It is likely
your LD_LIBRARY_PATH environment variable or CLASSPATH variable
does not include the directory containing the libsbmlj.jar file.