更改价值输入onclick女巫settimeout

发布于 2025-02-06 22:19:49 字数 760 浏览 2 评论 0 原文

单击按钮时,如何删除特殊符号(r $),或者如果它存在,则只需删除?

setTimeout(function clean() {
var input = document.getElementById('value')
input.value = input.value.replace(/[!$(){}[\]:;R<+?\\>]/g,'')
},3000)

我希望在单击按钮时,它也将删除

我到目前为止的

符号r $

setTimeout(function clean() {
var input = document.getElementById('value')
onclick = input.value = input.value.replace(/[!$(){}[\]:;R<+?\\>]/g,'')
}, 2000)
<input id="value" name="item_valor" value="R$ 14,99" >
remover simbolo pagseguro
<br>

<button class="" onclick="clean()">clean</button>

How can I remove special symbols (R$) when clicking the button, or simply remove if it exists?

setTimeout(function clean() {
var input = document.getElementById('value')
input.value = input.value.replace(/[!$(){}[\]:;R<+?\\>]/g,'')
},3000)

I would like that when clicking on the button, it would also remove the symbol R$

I have so far

setTimeout(function clean() {
var input = document.getElementById('value')
onclick = input.value = input.value.replace(/[!$(){}[\]:;R<+?\\>]/g,'')
}, 2000)
<input id="value" name="item_valor" value="R$ 14,99" >
remover simbolo pagseguro
<br>

<button class="" onclick="clean()">clean</button>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

开始看清了 2025-02-13 22:19:49

只需使用 方法,然后 string.trim()方法,以确保没有留下前导或落后空间。

const input = document.getElementById("value");
document.querySelector("button").addEventListener("click", function(){
  input.value = input.value.replace("R$","").trim();
});
<input id="value" name="item_valor" value="R$ 14,99" >
remover simbolo pagseguro
<br>

<button>clean</button>

Just use the String.replace() method and then the String.trim() method to ensure there are no leading or trailing spaces left behind..

const input = document.getElementById("value");
document.querySelector("button").addEventListener("click", function(){
  input.value = input.value.replace("R$","").trim();
});
<input id="value" name="item_valor" value="R$ 14,99" >
remover simbolo pagseguro
<br>

<button>clean</button>

情感失落者 2025-02-13 22:19:49

您的删除特殊符号的正则是正确的
Settimeout的唯一错误&amp;功能声明错误。

我已经解决了该代码错误。

下图下方可以工作,但是在函数清洁()中,由于Settimeout功能,它将等待1000ms。

function clean() {
  setTimeout(()=>{
    var input = document.getElementById('value')
    onclick = input.value = input.value.replace(/[!$(){}[\]:;R<+?\\>]/g,'');
    input.value = input.value.trim();
  }, 1000) // code inside setTimeout function will wait 1000ms to execute
}
<input id="value" name="item_valor" value="R$ 14,99" >
remover simbolo pagseguro
<br>

<button class="" onclick="clean()">clean</button>

Your regex for removing the special symbol is right,
only error was of setTimeout & function declaration error.

I have fixed that code error.

Below snippet will work, but in function clean() it will wait for 1000ms because of setTimeout function.

function clean() {
  setTimeout(()=>{
    var input = document.getElementById('value')
    onclick = input.value = input.value.replace(/[!$(){}[\]:;R<+?\\>]/g,'');
    input.value = input.value.trim();
  }, 1000) // code inside setTimeout function will wait 1000ms to execute
}
<input id="value" name="item_valor" value="R$ 14,99" >
remover simbolo pagseguro
<br>

<button class="" onclick="clean()">clean</button>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文