首页 > 解决方案 > Jquery .replaceWith() 没有用更新的文本正确更新

问题描述

我有一个表单,在提交之前我正在验证它。顾名思义,根据用户选择ValidateForm()验证表单并验证表单上的一组字段。ValidateRange(num)从我这里得到验证正在正确完成。

if在我检查结果的最后一个语句中,如果我使用 jQuery .append(),它工作正常,但我想用replaceWith()方法替换验证文本,但它没有发生。

result.message只是一个文本字符串,说明哪个字段存在验证问题并将用户指向它。

console.log()我可以看到验证指针消息正确出现,因为我用正确的值修复了字段,但由于某种原因,它<div id ='ValidationDiv'></div>一次又一次地用第一个指针消息替换。

function ValidateForm() {
    var result = {valid:true, message:"In-Progress"};

    if ($('#firstRangeRadioButton').is(':checked'))
        result = ValidateRange(1);
    else if ($('#secondRangeRadioButton').is(':checked'))
    {
        result = ValidateRange(1);
        if(result.valid == true)
            result = ValidateRange(2);
    } 
    else if ($('#thirdRangeRadioButton').is(':checked')) {
        result = ValidateRange(1);
        if (result.valid == true)
            result = ValidateRange(2);
        if (result.valid == true)
            result = ValidateRange(3);
    }

    if (result.valid == false)
    {
        console.log(result.message);
        message = "<div class=\"well well-sm\"><div><div class=\"blink\"><strong>Validation Failed: </strong>" + result.message + "</div></div></div>";
        $('#ValidationDiv').replaceWith(message);
        blink('.blink');
        console.log(message);

        return false;
    }
    else
        return true;
}

标签: javascriptjqueryreplacewith

解决方案


推荐阅读