0

嗨,当值来自数据库时,我想通过 java 脚本修剪左侧空间,我使用 jsp 标签获取值并在输入字段中输入这些值。当值的开始出现一些空间时,我面临问题,然后在所有输入字段中甚至没有打印单个值

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script>
    function getValue() {
        <jsp:useBean id="ProjectBO"
    class="com.nousinfo.tutorial.employee.service.model.bo.EmployeeProjectBO"
    scope="request" />

        document.getElementById("empNumber").value = '<jsp:getProperty property="employeeNumber" name="ProjectBO"/>';



document.getElementById("projectCode").value = '<jsp:getProperty property="projectCode" name="ProjectBO"/>';
        document.getElementById("startDate").value = '<jsp:getProperty property="startDate" name="ProjectBO"/>';
        document.getElementById("endDate").value = '<jsp:getProperty property="endDate" name="ProjectBO"/>';
        document.getElementById("role").value = '<jsp:getProperty property="role" name="ProjectBO"/>';
    }
</script>
</head>
<body onload="getValue()">

    <form id="employee" action="ProjectUpdateServlet" method="post">

        <table width="1254" height="74" border="0" align="center">
            <tr>
                <td width="970" height="68" align="center" bgcolor="#99CCFF"><h2>
                        <span class="style1">Project Detail</span>
                    </h2></td>
                <td width="274" height="68" align="center" bgcolor="#FFFFFF"><img
                    src="/image/Emp.jpg" width="190" height="92" /></td>
            </tr>
        </table>
        <p>
            <br />
        </p>
        <hr size="1" width="786">
        <table width="786" border="0" align="center">
            <tr>
            <td><input type="hidden" name="updateStatusProject" value="M" /></td>

            </tr>
            <tr>

                <td width="298">Employee Number:</td>
                <td><input type="text" id="empNumber" name="employeeNumber" readonly="readonly" />
                </td>
            <tr>
                <td>Project_Code:</td>
                <td><input type="text" name="projectCode" id="projectCode"  readonly="readonly"/>
                </td>
            </tr>
            <tr>
                <td>Start_date</td>
                <td><input type="text" name="startDate" id="startDate" /></td>
            </tr>
            <tr>
                <td>End_date</td>
                <td><input type="text" name="endDate" id="endDate" /></td>
            </tr>
            <tr>
                <td>Role</td>
                <td><input type="text" name="role" id="role" /></td>
            </tr>
        </table>

        <p>&nbsp;</p>
        <br />
        <table width="200" border="0" align="center">
            <tr>

                <td><center>
                        <input type="submit" name="submit" value="Save" onclick="self.close()"/>

                    </center></td>

                <td><center>
                        <input type="button" name="cancle" value="Cancel"
                            onclick="self.close()">
                    </center></td>
            </tr>
        </table>
        <hr size="1" width="786">
        <p>&nbsp;</p>
    </form>
</body>
</html>
4

2 回答 2

1

JavaScriptreplace可以接受搜索词的正则表达式,因此如果要删除前导空格:

str = str.replace(/^\s+/, '');

意思是“输入的^开始”,\s意思是“任何空白字符”,+意思是“前面的一个或多个”。所以总的来说,正则表达式的意思是“输入开头的空白字符”。我们将它们替换为''.

如果您想删除尾随空格,而不是^在开头,您将$在结尾处($=“输入结束”):

str = str.replace(/\s+$/, '');
于 2012-12-14T06:51:23.167 回答
1
String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
    return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
    return this.replace(/\s+$/,"");
}

// example of using trim, ltrim, and rtrim
var myString = " hello my name is ";
alert("*"+myString.trim()+"*");
alert("*"+myString.ltrim()+"*");
alert("*"+myString.rtrim()+"*");
于 2012-12-14T06:53:04.817 回答