weixin_33712881 2014-09-24 13:11 采纳率: 0%
浏览 5

用@标记用户

I'm currently building a @user tagging system. I have almost got it working with one little thing I can't get my head around. I know what the problem is, just can't think how I could rectify it.

So I put the @ sign in the div box enter a name, select it, and then its inserted in to the content area

<div cols="40" rows="5" name="newmsg" id="newmsg" placeholder="Say something <? echo $data['first']; ?>..." contenteditable="true"></div>

That all works fine. But after the name has inserted and I want to type it doesn't type straight away until I click on the div again and on every click of any key it searches and the msgbox with data shows content.

1.How could I stop the msgbox showing if the div already has a .addname selected and inputed. 2.And how can I change my code to not make it search after every key press.

I really don't know how facebook does it. All I know is they use a textarea and once you've inserted a name you can type normally straight away after that tagged person/s.

JS:

$(document).ready(function () {
    var start = /@/ig; // @ Match
    var word = /@(\w+)/ig; //@abc Match
    $("#newmsg").on("keyup", function () {
        var content = $(this).text(); //Content Box Data
        var go = content.match(start); //Content Matching @
        var name = content.match(word); //Content Matching @abc
        var dataString = 'searchword=' + name;
        //If @ available
        if (go.length > 0) {
            $("#msgbox").slideDown('show');
            $("#msgbox").html('<img src="load.gif" />');
            //if @abc avalable
            if (name.length > 0) {
                $.ajax({
                    type: "POST",
                    url: "textareausernameinsert.php", // Database name search 
                    data: dataString,
                    cache: false,
                    success: function (data) {
                        $("#msgbox").hide();
                        $("#msgbox").html(data).show();
                    }
                });
            }
        }
        return false;
    });

    //Adding result name to content box.
    $(".addname").live("click", function () {
        var username = $(this).attr('title');
        var old = $("#newmsg").html();
        var content = old.replace(word, "@"); //replacing @abc to (" ") space
        $("#newmsg").html(content);
        var E = "<a contenteditable='false' href='#'>" + username + "</a>";
        $("#newmsg").append(E);
        $("#msgbox").hide();

        INSERT AJAX HERE TO SEND CONTENT TO DATABASE, REMOVED AS NOT NEEDED.

    });
});
  • 写回答

1条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥30 如何降低hdfs中datanode的JVM内存用量
      • ¥20 SQL数据查询,子查询
      • ¥15 c++字符串分割问题
      • ¥15 vue+uniapp
      • ¥15 android freedom
      • ¥15 使用自定义的类型代替内置类型可行吗
      • ¥15 关于STM32的SPI和ENDAT接口编码器通信的问题
      • ¥15 关于#pdfbox#生成的PDF文件正常,转图片中文乱码的问题,如何解决?
      • ¥15 ADS中有关DAC控件的使用问题
      • ¥15 win11如何运行geoserver