document.getElementById("txt").value.replace(/\s/g, "")

解决方案 »

  1.   

    如果页面中的文本框name或是id已经确定,可以通过调用document.getElementById("文本框的id").value.replace(/\s/g,   "")或是document.getElementsByName("文本框的name").value.replace(/\s/g,   "")如果是对页面中所有的文本框进行操作,可以用下面这个方法:
    var txt = document.getElementsByTagName("text");
    for(var i = 0; i < txt.length; i++)
    {
       txt[i].value.replace(/\s/g, "");
    }
      

  2.   

    最常用的三种,中间空格不删除!String.prototype.Trim = function()
    {
        return this.replace(/(^\s*)|(\s*$)/g, "");
    }
    String.prototype.LTrim = function()
    {
        return this.replace(/(^\s*)/g, "");
    }
    String.prototype.RTrim = function()
    {
        return this.replace(/(\s*$)/g, "");
    }
    document.getElementById("txt").value.Trim()
      

  3.   

    <input type="text" onblur="removeSpace(this)" />
    <script>
    function removeSpace(obj)
    {
      obj.value=obj.value.replace(/\s+/g,"");
    }
    </script>
      

  4.   

    在onchange后自动去掉文本框中的所有空格,包括字符串中间的空格
      

  5.   

    在jsp中脚步过滤掉啊,使用String.Trim();