从 Javascript 中的 textarea 中删除某些文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17483329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 08:29:38  来源:igfitidea点击:

Remove certain text from textarea in Javascript

javascriptjqueryhtml

提问by Seth Earby

I need a little input on the best way to remove a certain word or phrase in a text area. I have an input field entitled input. I want to search that area for the word that is put into a form field called nameand then removed from input. None of the code I have tried works, and most search results yield irrelephant.

我需要一些关于删除文本区域中某个单词或短语的最佳方法的输入。我有一个名为input. 我想找寻区域被放入称为表单字段的字name,然后取出从input。我尝试过的代码都不起作用,大多数搜索结果都产生了无关紧要的结果。

function delete() {
    var name = document.getElementById("name").value;
    var textarea = document.getElementById("input").value;
    var data = textarea.length;
    for (var i = 0; i < data.length; i++) {
        if (textarea.substr(i, data) == name) {
            textarea.value = "";
        }
    }
}

What I tried earlier with no luck.

我之前尝试过但没有运气。

回答by Mr_Green

Not sure what you are trying to do, but this is the correct code: (there are many mistakes in your code)

不确定您要做什么,但这是正确的代码:(您的代码中有很多错误

function deleteValue() {  //instead of "delete"
    var name = document.getElementById("name");
    var textarea = document.getElementById("result");  //intead of "input"
    var data = textarea.value;
    for (var i = 0; i < data.length; i++) {
        if (data.substr(i, data) == name.value) {
            textarea.value = "";
        }
    }
}

Some points to consider:

需要考虑的几点:

  • Change your text input name from inputto something else because it is a tag name.
  • You can't have a function name deletebecause it is a reserved word in javascript. use something else like deleteValue.
  • 将您的文本输入名称从更改input为其他名称,因为它是tag name.
  • 你不能有函数名,delete因为它是 javascript 中的保留字。使用其他类似的东西deleteValue

UPDATE:

更新

I think this is what you are looking for..

我想这就是你要找的..

function deleteValue() {
    var name = document.getElementById("name");
    var textarea = document.getElementById("result");
    textarea.value = textarea.value.replace(name.value, "");
}

Working Fiddle

工作小提琴

回答by maketest

You can add replaceAllfunction to String object. If you want to remove all occurrences of specific text.

您可以replaceAll向 String 对象添加函数。如果要删除所有出现的特定文本。

(function() {   
    if (!String.replaceAll) {
        String.prototype.replaceAll = function replaceAll(replace, value) {
            return this.replace(new RegExp(replace, 'g'), value);
        };
}
}());

And modify deletefunction should look like this:

修改delete函数应该是这样的:

function deleteValue() {
    var name = document.getElementById('name');
    var textarea = document.getElementById('result');
    var data = textarea.value;
    textarea.value = textarea.value.replaceAll(name.value, "");
} 

回答by Brewal

This is the jQuery solution (I provide it since you tagged jQuery) :

这是 jQuery 解决方案(因为您标记了 jQuery,所以我提供了它):

jsFiddle

js小提琴

// to execute code when the page is entierly loaded
$(document).ready(function(){
    // click event on the button
    $('#myButton').click(function(){
        // get the content of the #result textarea
        val = $('#result').val();
        // remove all occurence of content of #name in #result
        $('#result').val(val.replace($('#name').val(), ""));
    });
});