我试图在选择框中删除我的选项中的单引号,但下面似乎没有工作: $(function(){ $("#agencyList").each(function() { $("option", $(this)).each(function(){ var cleanValue = $(this).text(); cleanValue.replace("'",""); $(t
$(function(){
$("#agencyList").each(function() {
$("option", $(this)).each(function(){
var cleanValue = $(this).text();
cleanValue.replace("'","");
$(this).text(cleanValue);
});
});
});
它仍然有单引号. select是使用JSTL forEach循环构建的.任何人都可以看到可能出错的地方?
您必须使用cleanValue = cleanValue.replace(…)来分配新值.此外,如果要替换所有单引号,请使用全局RegEx:/’/ g(替换所有出现的单引号):$(function(){
$("#agencyList").each(function() {
$("option", this).each(function(){
var cleanValue = $(this).text();
cleanValue = cleanValue.replace(/'/g,"");
$(this).text(cleanValue);
});
});
});
另一个调整:
>用此替换$(this),因为没有必要将此对象包装在jQuery对象中.
>您的代码可以进一步优化我的合并两个选择器:
$(function(){
$("#agencyList option").each(function() {
var cleanValue = $(this).text();
cleanValue = cleanValue.replace(/'/g,"");
$(this).text(cleanValue);
});
});
