【问题标题】:Javascript : Replacing the blank spaces of a specific keyword in the textareaJavascript:替换文本区域中特定关键字的空格
【发布时间】:2017-11-10 10:51:50
【问题描述】:

我需要您帮忙编写一个 javascript 函数来用文本区域中的其他单词替换特定单词。

我想在 textarea 中查找并替换输入文本上放置的单词之间的空格:

例如:

<html>
<head>
<title>Replace Specific words in textarea</title>
</head>
<body>
<Form name="frm" method="post" id="frm">
<TextArea name="txtArea1" cols="85" rows="10" ID="Textarea1">
While it was clear that forwarding the material was naughty (and the "culprits" had their wrists smacked), there.
This is one of the#main#ways of using link events. If you have not seen rollover images before, they are images 
</TextArea><br>
Input keywords: <Input type="text" name="findwords" value="the main ways" id="text1">
<Input type="submit" name="btnSubmit" value="Submit" id="submit">
</form>
</body>
</html>

任何帮助将不胜感激

鲍勃

【问题讨论】:

  • 更精确地了解您想要的内容。替换后的结果是什么?
  • 将在textarea中找到的输入关键字的空格替换为“#” - 只是关键字的空格:例如:输入文本:“主要方式”
  • 考虑到该关键字在 textarea 中的实例...

标签: javascript input replace textarea keyword


【解决方案1】:

不确定是否真的理解,但试试这个:

// Execute instructions when the button with ID "submit" is clicked.
document.getElementById('submit').addEventListener('click', function()
{
    // Get user inputs.
    var search = document.getElementById('text1').value;
    // Replace spaces and tabs with a regular expression.
    var alteredSearch = search.replace(/\s/g, '#');
    // Get the textarea content.
    var look = document.getElementById('Textarea1').value;

    // Finally, replace the textarea content.
    document.getElementById('Textarea1').value = look.replace(new RegExp(search, 'g'), alteredSearch);
});

我看到你有一个表格。目前,如果您点击提交按钮,您的页面将重新加载,并且不会执行任何操作。如果您希望 javascript 工作,请将输入的 type 属性设置为 button

【讨论】:

  • 好的。我将输入按钮设置为“按钮”,但是当我将您的 JS 代码放在
  • 把你的
  • 当然没用,您还删除了输入的 id 属性。 type 是“按钮”,id 是“提交”。
  • 哎呀!它在我的台式计算机上运行良好,但在我的智能手机和平板电脑(以及其他执行 javascript 的文件)中运行良好。你知道为什么以及如何解决它吗?
猜你喜欢
  • 1970-01-01
  • 2013-09-14
  • 1970-01-01
  • 2011-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多