2012-05-18 47 views
1

我有以下下拉列表:如何更换某些词在所有下拉列表元素

<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'); 

但这取代一切课程!有任何想法吗?

感谢,

回答

1

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

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

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

有迹象表明,有资格作为单词边界的三个不同位置:

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

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

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

希望这有助于

jQuery代码

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

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

谢谢,这是诀窍:) – Paul

+0

@Puneet很高兴帮助! :) –

+0

这可以应用于替换下拉菜单中字符串中的字符吗?说一个'*'? – Doidgey

0

用这个代替

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

你的过滤函数发现其中包含文本 '标准' 的元素,然后替换其文本
而是可以使用

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

谢谢,但没有做我所需要的! – Paul

0

只需使用each

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

相关问题