开发者

Jquery - replace text in Val()

开发者 https://www.devze.com 2022-12-21 22:27 出处:网络
I\'m trying to replace parts of a Val() in jquery with some text but am not sure how to do this. $(this).val()开发者_StackOverflow社区 returns \"# number of....\" - I would like to replace # with cust

I'm trying to replace parts of a Val() in jquery with some text but am not sure how to do this. $(this).val()开发者_StackOverflow社区 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


You can pass a function to .val() in 1.4+ like this:

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


Do not forget to add a .val() at the end :

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

精彩评论

暂无评论...
验证码 换一张
取 消