gpt4 book ai didi

jquery - 用 jquery 替换反斜杠

转载 作者:行者123 更新时间:2023-12-01 04:09:55 24 4
gpt4 key购买 nike

在下面的表单中,如果我尝试使用 .replace() 替换反斜杠,则会导致错误:

<form>
<input id="plop" type="text" value="" style="width:500px;">
<button id="button" type="button" value="" onclick="pouet()">Plop</button>
</form>

<script>
function pouet()
{
var a1 =$("#plop").val().replace(/\\/g,'/');
document.write(a1); // added for testing
}
</script>

ReferenceError: Can't find variable: pouet

如果我尝试替换另一个字符(例如 - 或/),效果很好。似乎\中的第一个反斜杠没有转义第二个反斜杠......我不明白为什么。

你知道如何全局替换 var a1 中的每个反斜杠吗?谢谢

最佳答案

$(document).ready(function() {
$("#button").click(function(e) {
e.preventDefault();
var a1 = $("#plop").val().replace(/\\/g,"/");
$("#plop").val(a1);
$.post("path/to/php/script", {a1:a1}, function(data) {
// do something with php return data
});
});
});

如果您只是在用户单击一个按钮时运行一些代码,则甚至不必编写自定义函数。

Tested and working .

关于jquery - 用 jquery 替换反斜杠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21997969/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com