ajax ,html and javascript

ajax ,html and javascript

本文关键字:javascript and html ajax      更新时间:2023-09-26

如何替换两条消息(好的和坏的)?假设字段中的数据是否正确显示良好,如果不是 - 显示不好,但比我再次更改它,因此消息将相应更改。

(我对这一切很陌生)

$(document).ready(function () {
        $('#button').click(function(e){
            var result = true,
                ok = '<p>good</p>',
                failed = '<p>bad</p>',
                err;
            $('input').each(function(index,obj){
                var obj =   $(obj);
                if (obj.attr('id') == 'button'){
                    return false;
                }
                err =  validateInput(o);
                markInput(o,err);
                if (!err){
                    result = false;
                }
            });
            if (result){
                $('#myForm').after(good);
                $.ajax();
            }
            else{
                $('#myForm').after(bad);
                $.ajax();
            }
        });

而不是使用 .after 有一个元素作为消息的占位符并更新其 html:

<form>
    <!-- all your code -->
    <p id="message"></p>
</form>
// JavaScript
$("#myForm #message").html(good);
// also just `$("#message")` will work, and you can probably leave
// the `<p>` parts off of `good` and `bad`

更改:

 if (result){
                $('#myForm').after(good);
                $.ajax();
            }
            else{
                $('#myForm').after(bad);
                $.ajax();
            }

自:

 if (result){
                $('#myForm').after(ok);
                $.ajax();
            }
            else{
                $('#myForm').after(failed);
                $.ajax();
            }

没有使用你创建的变量,名为"ok"和"failed",而是在不存在的.after()中输入了"好"和"坏"。还可以考虑使用 $('#messagePlaceHolder').replace(ok or failed) 而不是 .after,其中 messagePlaceHolder 是一个 .