Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 每次Append语句都会继续执行_Jquery - Fatal编程技术网

Jquery 每次Append语句都会继续执行

Jquery 每次Append语句都会继续执行,jquery,Jquery,警报每次都会继续追加,而我只希望它追加一次 这就是我的整个代码的样子,仍然在学习 // 1. listen for submit jQuery('.save').submit(function(event) { // 2. get the form data var formData = { 'editor_content' : jQuery('.jqte_editor').text() }; // grab the form url

警报每次都会继续追加,而我只希望它追加一次

这就是我的整个代码的样子,仍然在学习

// 1. listen for submit 
jQuery('.save').submit(function(event) {

    // 2.  get the form data
    var formData = {
        'editor_content' : jQuery('.jqte_editor').text()
    };
    // grab the form url
    url = jQuery('.save').attr('action');

     //  Show spinner as request start
    $('.spinner').show();

    //  4. process the form

    jQuery.ajax({
        type        : 'POST', // define the type of HTTP verb we want to use (POST for our form)
        url         : url, // the url where we want to POST
        data        : formData, // our data object
        dataType    : 'json', // what type of data do we expect back from the server
                     encode          : true
    })
        // using the success promise callback
        .success(function(data) {
            // stop spinner as request completes
                    $('.spinner').hide();

            // here we will handle errors and validation messages
            if(! data.success)
            {
//每次我按下submit按钮时,它都会继续追加

                $('.save').append('<div class="warning alert">'+data.editor_content+'</div>');
            }
            });

    // stop the form from submitting the normal way and refreshing the page
    event.preventDefault();
});
$('.save').append(''+data.editor\u content+'');
}
});
//停止表单以正常方式提交并刷新页面
event.preventDefault();
});

您可以验证是否已经添加了
.alert
框,并防止再次添加

if(!data.success && $('.save > .warning.alert').size() == 0){
   $('.save').append('<div class="warning alert">'+data.editor_content+'</div>');
}
如果(!data.success&&$('.save>.warning.alert').size()==0){
$('.save').append(''+data.editor\u content+'');
}
var-warned=false;
如果(!data.success){
如果(!警告)
$('.save').append(“”+data.editor_content+“”);
警告=正确;
}
}

您在哪里设置
数据。成功
?是否可以显示更多代码?…假设
中没有其他内容。保存
元素。最好检查
.save>.warning.alert
元素是否已存在。
var warned = false;
if (! data.success){
    if (! warned)
       $('.save').append('<div class="warning alert">' + data.editor_content + '</div>');
       warned = true;
    }
}