Jquery - 如何添加消息并清除前一个消息?

时间:2012-09-18 09:42:47

标签: jquery

我的网页如下;

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<SHP.Models.AdditionalDepartmentsViewModel>" %>
<%@ Import Namespace="SHP.Helpers" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Additional Departments</title>
</head>
<body>
    <p>For SHP <%: Model.Employee.Forename %> <%: Model.Employee.Surname %> has been assigned to 
        <%: Model.Employee.BusinessUnit.BusinessUnitName %>.</p>
    <p>For MNet <%: Model.Employee.Gender.GetThirdPersonSingularPronoun()%> you can assign the Departments here as well.</p>
    <div id="TableBusinessUnits">
        <div id="leftSide">
            <table>
                <tr>
                    <th>Departments</th>
                    <th></th>
                </tr>
                <%: Html.EditorFor(model => model.Departments) %>
            </table>
        </div>
        <div id="rightSide" style="padding-right:50px;">
            <table>
                <tr>
                    <th>Divisions</th>
                    <th></th>
                </tr>
                <%: Html.EditorFor(model => model.Divisions) %>
            </table>                        
        </div>
    </div>
</body>
</html>
<script type="text/javascript">
    $(document).ready(function () {
        var elementId;
        $('tr input[type="checkbox"]').click(function () {
            elementId = $(this).attr('id');
            var url = $(this).attr('data-url');
            var employeeId = $(this).attr('data-employeeId');
            var businessUnitId = $(this).attr('data-businessUnitId');
            var selectedFlag = $(this).is(':checked');

            dataService.saveSelection(
            employeeId,
            businessUnitId,
            selectedFlag,
            elementId,
            SavedSetting,
            url
        );
        });
    });

    SavedSetting = function (data) {
        $(data.ElementId).after('<span class="error">' + data.Message + '</span>');
    };
</script>

EditorFor命令为此编辑器模板提供循环;

<tr>
    <td><%: Model.BusinessUnit.BusinessUnitName %></td>
    <td>
    <%: Html.CheckBoxFor( 
            x => x.Selected,
            new RouteValueDictionary 
            { 
                { "data-url", Url.Action("AddBusinessUnitForEmployee", "DataService") }, 
                { "data-employeeId", Model.EmployeeId }, 
                { "data-businessUnitId", Model.BusinessUnit.BusinessUnitId } 
            } 

       ) %> 

    </td>
</tr>

上面的JQuery的最后一块 -

SavedSetting = function (data) {
            $(data.ElementId).after('<span class="error">' + data.Message + '</span>');
        };
  • 在表格中的复选框后面添加一条消息。我想要做的是清除上一条消息(如果存在)。我该怎么做?

2 个答案:

答案 0 :(得分:2)

定义 -

$('.error').empty();

在其中添加消息之前

答案 1 :(得分:0)

您也可以使用它。
  $( '错误 ')文本('');