我写了一段替换 代码的脚本,测试成功:如下:
 <script   language="javascript">  
   
  String.prototype.trim   =   function(){  
return this.replace(/&nbsp;/g,"");
  }  
  alert("标&nbsp;题&nbsp;&nbsp;".trim());
    
  </script>但是,我在我的一个项目中用到上面代码,把Option中的Text值以","号分隔起来,但并未成功替换掉&nbsp;不知为什么.代码如下:
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>标&nbsp;题&nbsp;&nbsp;
</option>
<option value="keywords" style="font-size:7pt;color:#385486">关键字&nbsp;&nbsp; 
</option>
</select> 
<input   type="hidden"   name="v_ids" value=""> 
</td>

解决方案 »

  1.   

    String.prototype.ReplaceAll = stringReplaceAll;function  stringReplaceAll(AFindText,ARepText){
      raRegExp = new RegExp(AFindText,"g");
      return this.replace(raRegExp,ARepText)
    }
      

  2.   

    你先把document.selectform.select.options[i].text.trim();  的值alert出来
    看看是不是已经替换好的 最好自己写个替换的函数,不要更改trim的方法
      

  3.   

    什么错误?
    把trim()去掉后好使么?或是trim的函数体没放在用到之前
      

  4.   

    前面说的明白,这个测试结果正确:<script   language="javascript">  
       
      String.prototype.trim   =   function(){  
    return this.replace(/&nbsp;/g,"");
      }  
      alert("标&nbsp;题&nbsp;&nbsp;".trim());
        
      </script>
    但为什么同样的trim函数,却不能把Option中的&nbsp;替换掉.Alert显示没有替换掉,但
    把Option的各个值却用逗号分开了.证明subm()执行了,但为什么在一个网页中就不行了,单独
    的JavaScript脚本就执行成功.
      

  5.   

    你alert()看看你传递的值是什么啊,还有&nbsp;吗?注意select对象中value和text的两个属性的区别
      

  6.   

    注意到了Text,和Value的区别,但是就是不行,最近没时间研究,有空再研究一下,如果有高手希望看一下.
    sureyor() 的方法我可以试一下.