简体   繁体   中英

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. $(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

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");
});

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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