【问题标题】:Replace error list instead of append Json替换错误列表而不是附加 Json
【发布时间】:2017-11-30 12:31:09
【问题描述】:

嗨,当数据输入错误时,我有 bleow json 代码来验证表单,如果在单击创建按钮时数据多次输入错误,它会再次附加所有数据并且仍然有第一组错误,是有没有办法在单击按钮时删除当前错误并用更新的错误替换它们?

        function CreateChild() {
        $("#formErrors").remove();
        $.ajax({
            type: "POST",
            contentType: "application/json",
            url: rootURL,
            dataType: "json",
            data: formToJSONCreate(),
            success: function (data, textStatus, jqXHR) {
                alert('Child Added Succesfully');
                clearCreateForm();
                displayList();
            },
            error: function (jqXHR, textStatus, errorThrown) {
                var errors = JSON.parse(jqXHR.responseText).ModelState
                var errorText = Object.keys(errors).map((key) => errors[key])
                console.log(Object.keys(errorText))

                $('#formErrors').append(errorText.join(' <br>'))

            }
        });
    }

【问题讨论】:

  • 是的,使用 .html() 而不是 .append()。 RTM - api.jquery.com/html
  • 做到了,不让我选这个作为正确答案
  • 那是因为我把它写成评论。我现在添加了一个完整的答案,你可以接受 - 谢谢:-)

标签: asp.net json ajax


【解决方案1】:

为此,只需使用.html() 而不是.append()

$('#formErrors').html(errorText.join(' <br>'));

有关此方法的详细信息,请参阅http://api.jquery.com/html/。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-21
    • 1970-01-01
    • 2018-07-17
    • 1970-01-01
    • 2014-07-29
    • 1970-01-01
    • 1970-01-01
    • 2016-05-29
    相关资源
    最近更新 更多