简体   繁体   English

Jquery - 替换Val()中的文本

[英]Jquery - replace text in Val()

I'm trying to replace parts of a Val() in jquery with some text but am not sure how to do this. 我试图用一些文本替换jquery中Val()的部分,但我不知道如何做到这一点。 $(this).val() returns "# number of...." - I would like to replace # with custom text, but Val doesn't seem to have a replace() function. $(this).val()返回“ $(this).val() of ....” - 我想用自定义文本替换# ,但Val似乎没有replace()函数。 I'm quite new to JQuery, so I might be missing something obvious. 我对JQuery很陌生,所以我可能会遗漏一些明显的东西。 Thanks for any help 谢谢你的帮助

You can pass a function to .val() in 1.4+ like this: 您可以将函数传递.val() ,如下所示:

$("#mySelector").val(function(i, v) { //index, current value
  return v.replace("#","Custom Text");
});

不要忘记在末尾添加.val():

$("#mySelector").val(function(i, v) {return v.replace("#","Custom Text");}).val();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM