如何替换所有DropDown List元素中的某些单词

时间:2012-05-18 10:59:42

标签: jquery replace

我有以下下拉列表:

 <select id="dropdown">
      <option value="AB">Alberta Standards</option>
      <option value="BC">British Columbia Standards</option>
 </select>

尝试将标准更改为课程而不改变任何其他内容。

这就是我所拥有的:

$('#dropdown option').filter(function() { return /Standards/.text($(this).text());}).text('Curriculum');

但这取代了课程的一切!有什么想法吗?

谢谢,

5 个答案:

答案 0 :(得分:1)

Hiya 工作演示 http://jsfiddle.net/fppeZ/ http://jsfiddle.net/fppeZ/1/

好读:http://www.regular-expressions.info/wordboundaries.html

  

元字符\ b是一个像插入符和美元符号的锚。   它匹配一个称为“单词边界”的位置。这场比赛   是零长度。

     

有三种不同的职位符合词边界:

     
      
  • 在字符串中的第一个字符之前,如果第一个字符是单词字符。

  •   
  • 在字符串中的最后一个字符之后,如果最后一个字符是单词字符。

  •   
  • 字符串中的两个字符之间,其中一个是单词字符,另一个不是单词字符....

  •   

HOpe这有帮助,

Jquery代码

$('#dropdown option').each(function() {

     $(this).text($(this).html().replace(/\bStandards\b/g, 'Curriculum'));
});​

答案 1 :(得分:0)

改为使用

$('#dropdown option').each(function() {
    $(this).text($(this).text().replace( /Standards/gi, 'Curriculum'));
});

答案 2 :(得分:0)

$('#dropdown option').each(function() {
    $(this).text($(this).text().replace(/Standards/g, 'Curriculum'));
});

答案 3 :(得分:0)

您的过滤器功能会查找包含文本“标准”的元素,然后替换其文本 相反,你可以使用

$('#dropdown option').each(function(i,v){
  $(v).text().replace( /Standards/gi, 'Curriculum');
});

答案 4 :(得分:0)

只需使用each

$("#dropdown option").each(function () {
    $(this).text($(this).text().replace("Standards", "Curriculum")​​​​​​​​​​​​​​​​​​);
​​​​​​});