简体   繁体   English

如何避免多次追加

[英]How to avoid append multiple times

$('.btn-default').click(function()
{
    var foldername = $('#file_title').val();
    $.post ('<?php echo  base_url(); ?>ClientCont/List_Files/inserttitle',{
        file_title:foldername
    },function(data){
        console.log(data);
    if(data['exist']== true ){
        if($('#notifme').length != 0){
            $('#notifme').append("Folder name already exist!");
            $('#notifme').show();
        }
    },'json');
}); 

Just wanted to ask, the code above is used to create a folder if the folder exists, I will notify the user that the folder is already in the database, but if the user click the button multiple times append execute multiple times. 只是想问一下,上面的代码用于创建一个文件夹(如果该文件夹存在),我将通知用户该文件夹已经在数据库中,但是如果用户多次单击该按钮,则会追加执行多次。 Please help how to avoid that #notifme is the name of my id for my alert thanks 请帮助避免#notifme是我的ID名称,以表示感谢

您可以使用html()代替append()

$('#notifme').html("Folder name already exist!");

The append() method inserts specified content at the end of the selected elements ie It keeps on adding content on each event. append()方法在所选元素的末尾插入指定的内容,即,它继续在每个事件上添加内容。 html( htmlString ) sets the HTML contents of each element in the set of matched elements ie it will replace the html of selected element with supplied html string. html( htmlString )设置匹配元素集中每个元素的HTML内容,即它将用提供的html字符串替换所选元素的html。 And text( text ) set the content of each element in the set of matched elements to the specified text ie it will replace the content of selected element. text( text )将匹配元素集中每个元素的内容设置为指定的文本,即它将替换所选元素的内容。 So you can use either html( htmlString ) or text( text ) to set the content in element. 因此,您可以使用html( htmlString )text( text )来设置element中的内容。

 $('button').on('click', function(){ $('#notifme1').text("This is set by text()"); $('#notifme2').html("<b>This is set by html()</b>") }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="notifme1"> </div> <div id="notifme2"> </div> <button>Click me</button> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM