我写了一段替换 代码的脚本,测试成功:如下:
<script language="javascript">
String.prototype.trim = function(){
return this.replace(/ /g,"");
}
alert("标 题 ".trim());
</script>但是,我在我的一个项目中用到上面代码,把Option中的Text值以","号分隔起来,但并未成功替换掉 不知为什么.代码如下:
function subm(){ for(var i = 0 ; i<document.selectform.select.length; i++ ){ document.selectform.v_ids.value += document.selectform.select.options[i].text.trim(); if ( i !=(document.selectform.select.length-1) ){document.selectform.v_ids.value += ',';}
}
alert(document.selectform.v_ids.value);
return true;
}
<select name="select">
<option value="title" style="font-size:8pt;color:#385486"selected>标 题
</option>
<option value="keywords" style="font-size:7pt;color:#385486">关键字
</option>
</select>
<input type="hidden" name="v_ids" value="">
</td>
<script language="javascript">
String.prototype.trim = function(){
return this.replace(/ /g,"");
}
alert("标 题 ".trim());
</script>但是,我在我的一个项目中用到上面代码,把Option中的Text值以","号分隔起来,但并未成功替换掉 不知为什么.代码如下:
function subm(){ for(var i = 0 ; i<document.selectform.select.length; i++ ){ document.selectform.v_ids.value += document.selectform.select.options[i].text.trim(); if ( i !=(document.selectform.select.length-1) ){document.selectform.v_ids.value += ',';}
}
alert(document.selectform.v_ids.value);
return true;
}
<select name="select">
<option value="title" style="font-size:8pt;color:#385486"selected>标 题
</option>
<option value="keywords" style="font-size:7pt;color:#385486">关键字
</option>
</select>
<input type="hidden" name="v_ids" value="">
</td>
raRegExp = new RegExp(AFindText,"g");
return this.replace(raRegExp,ARepText)
}
看看是不是已经替换好的 最好自己写个替换的函数,不要更改trim的方法
把trim()去掉后好使么?或是trim的函数体没放在用到之前
String.prototype.trim = function(){
return this.replace(/ /g,"");
}
alert("标 题 ".trim());
</script>
但为什么同样的trim函数,却不能把Option中的 替换掉.Alert显示没有替换掉,但
把Option的各个值却用逗号分开了.证明subm()执行了,但为什么在一个网页中就不行了,单独
的JavaScript脚本就执行成功.
sureyor() 的方法我可以试一下.