使用jQuery设置Label的文本

时间:2017-01-22 15:31:47

标签: jquery

我正在尝试使用jQuery库的(document).ready方法设置标签的文本:

<script src="~/Scripts/jquery-1.4.4.js"></script>
<script>
    $j = jQuery.noConflict();
    $j(document).ready(function () {
        $j("#lblStatus").text("Must input EmployeeID");
    });
</script>
using (Html.BeginForm())
{
    <label id="lblStatus">--</label>
    @Html.LabelFor(model => model.employeeID, new { htmlAttributes = new { @class = "form-control" } })
    @Html.EditorFor(model => model.employeeID, new { htmlAttributes = new { @class = "form-control" } })
    <br />
    <input type="submit" value="Search" class="btn btn-default" />
}

HTML是:

<script src="/Scripts/jquery-1.4.4.js"></script>
<script>
    $j = jQuery.noConflict();
    $j(document).ready(function () {
        $j("#lblStatus").text("Must input EmployeeID");
    });
</script>

<h2>Search Employee</h2>

    <form action="/salary/searchEmployee" method="post">        <label id="lblStatus">--</label>
<label for="employeeID" htmlAttributes="{ class = form-control }">employeeID</label><input class="input-validation-error form-control text-box single-line" data-val="true" data-val-remote="User with this employeeID already exists." data-val-remote-additionalfields="*.employeeID,*.id" data-val-remote-type="POST" data-val-remote-url="/salary/cnicExist" data-val-required="Employee ID is required." id="employeeID" name="employeeID" type="text" value="" />        <br />
            <input type="submit" value="Search" class="btn btn-default" />
    </form><a href="/salary">Back</a>
            <hr />
            <footer>
                <p>&copy; 2017 - HRMS</p>
            </footer>
        </div>

        <script src="/Scripts/jquery-1.10.2.min.js"></script>
        <script src="/Scripts/bootstrap.min.js"></script>

一个非常简单的代码但无法弄清楚为什么在文档加载时它没有将标签的(lblStatus)文本设置为“必须输入EmployeeID”。有任何想法吗?

1 个答案:

答案 0 :(得分:0)

我认为问题不在于jQuery的 document.ready 方法 您的剃刀代码可能会生成错误的HTML。您可以尝试更新剃刀代码,如下所示。

using (Html.BeginForm())
{
    @Html.Label("--", new { id = "lblStatus", @class = "form-control" })
    @Html.LabelFor(model => model.employeeID, new { @class = "form-control" } )
    @Html.EditorFor(model => model.employeeID, new { @class = "form-control" } )
    <br />
    <input type="submit" value="Search" class="btn btn-default" />
}