简体   繁体   English

替换html textarea中的文本

[英]Replace text in html textarea

I have this script that matches a text on keyup in a textarea if between the minus sign. 我有这个脚本,如果在减号之间,则与textarea中keyup上的文本匹配。

    $('#notes').keyup(function() { // notes is the id of the textarea
        var regex = /\-([^\)]+)\-/gmi; // it gets the text between minus sign. Example: -text-
        var myValue = $(this).val();
        if(myValue.match(regex)){
            var reference = myValue.match(regex);
            $.ajax({
                async: false, 
                type: "POST",
                url: "scripts/script.php",
                data: { "reference" : reference },
                success: function(data) {   
                    // I need to replace the text matched by the regex with data in the textarea.
                    // I need to stop the ajax calling after success or call it only if regex is matched
                }
            }); 
        }
    });

When text is matched by the regex it send an ajax post call to a script that search the world in a database and returns a definition. 当文本与正则表达式匹配时,它将向脚本发送ajax post调用,该脚本在数据库中搜索世界并返回定义。 I need to replace the text matched by the regex with data, the definition extracted by the database. 我需要将正则表达式匹配的文本替换为数据,即数据库提取的定义。

Additionally I'd like to launch the ajax POST call only if regex is matched. 另外,我只想在匹配正则表达式时启动ajax POST调用。 It works just the first time. 它只是第一次工作。 After first match it still sending the call for each keyup. 第一次比赛后,它仍会为每个按键发送呼叫。

Try the below code. 试试下面的代码。

var triggerTime;
$("#notes").keyup(function() { // notes is the id of the textarea
        clearTimeout(triggerTime);
        var myValue = $(this).val();
        triggerTime = setTimeout(function(){
            var regex = /\-([^\)]+)\-/gmi; // it gets the text between minus sign. Example: -text-
            if(myValue.match(regex)){
                var reference = myValue.match(regex);
                $.ajax({
                    async: false, 
                    type: "POST",
                    url: "scripts/script.php",
                    data: { "reference" : reference },
                    success: function(data) {   
                        // I need to replace the text matched by the regex with data in the textarea.
                        // I need to stop the ajax calling after success or call it only if regex is matched
                    }
                }); 
            }
        }, 3000);// Call request in 3 second
    });

This is the optimized version of your code. 这是代码的优化版本。 it will wait for users to complete, and in inactivity of 3 seconds, it will generate an Ajax call. 它会等待用户完成操作,并且在3秒钟内处于不活动状态时,它将生成Ajax调用。

you can change the frequency to 2000 OR 1000 (2 second and 1 second respectively). 您可以将频率更改为2000或1000(分别为2秒和1秒)。

I solved the problem adding contenteditable="true" to the textarea. 我解决了将contenteditable =“ true”添加到文本区域的问题。 Below the final jquery code: 在最终的jquery代码下面:

    var textInput = $("#notes"); // the ID of the textarea
    var triggerTime;
    $("#notes").keyup(function() { // notes is the id of the textarea
        clearTimeout(triggerTime);
        var myValue = $(this).text();
        triggerTime = setTimeout(function(){
            var regex = /\-([^\)]+)\-/gmi; // it gets the text between minus sign. Example: -text-
            if(myValue.match(regex)){
                var newValue;
                var reference = myValue.match(regex);
                $.ajax({
                    async: false, 
                    type: "POST",
                    url: "scripts/parser.php",
                    data: { "reference" : reference },
                    success: function(data) {       
                        newValue = data;    
                        console.log(newValue);
                        $('.textarea').html(function() {
                            return $(this).text().replace(reference[0], '<strong>' + newValue + '</strong>');
                        });
                    }
                }); 
            }
        }, 1000);// Call request in 1 second
    });

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

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