Jquery - 替换 Val() 中的文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2391274/
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
Jquery - replace text in Val()
提问by o-logn
I'm trying to replace parts of a Val() in jquery with some text but am not sure how to do this. $(this).val()
returns "# number of...." - I would like to replace #
with custom text, but Val doesn't seem to have a replace()
function. I'm quite new to JQuery, so I might be missing something obvious.
Thanks for any help
我正在尝试用一些文本替换 jquery 中的 Val() 部分,但我不确定如何执行此操作。$(this).val()
返回“# number of....” - 我想#
用自定义文本替换,但 Val 似乎没有replace()
函数。我对 JQuery 很陌生,所以我可能会遗漏一些明显的东西。谢谢你的帮助
回答by Nick Craver
You can pass a function to .val()
in 1.4+ like this:
你可以像这样在 1.4+ 中传递一个函数.val()
:
$("#mySelector").val(function(i, v) { //index, current value
return v.replace("#","Custom Text");
});
回答by Zitun
Do not forget to add a .val() at the end :
不要忘记在最后添加一个 .val() :
$("#mySelector").val(function(i, v) {return v.replace("#","Custom Text");}).val();