日期:2014-05-18  浏览次数:20744 次

JavaScript的奇怪问题.
我写了一段替换 代码的脚本,测试成功:如下:
  <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>

------解决方案--------------------
搞不定,帮顶
------解决方案--------------------
String.prototype.ReplaceAll = stringReplaceAll;

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

------解决方案--------------------
等待答案
------解决方案--------------------
你先把document.selectform.select.options[i].text.trim(); 的值alert出来
看看是不是已经替换好的 最好自己写个替换的函数,不要更改trim的方法
------解决方案--------------------
什么错误?
把trim()去掉后好使么?

或是trim的函数体没放在用到之前
------解决方案--------------------
你alert()看看你传递的值是什么啊,还有&nbsp;吗?注意select对象中value和text的两个属性的区别