1. 数字验证
function onlyNum()
{
var s=document.getElementById("num").value;
if(isNaN(s)){
alert("对不起,只允许输入数字!")
return false;
}
}
<body>
<div>验证数字:
<input type="text" id="num">
<input type="button" value="提交" onclick="onlyNum();">
</div>
</body>
2. JS去掉前后空格
首先要定义去掉空格的函数:
String.prototype.trim= function(){
<%--用正则表达式将前后空格--%>
<%--用空字符串替代。--%>
return this.replace(/(^\s*)|(\s*$)/g, "");
}
在其他的js中直接引用该js就可:…value.trim();
3. 使用js提交Action
function actionSubmit(){ document.getElementById("a").action="${basePath }score/UpdateScoreAction.do?method=updateScore";
document.getElementById("a").submit(); }
其中a为Form表单的id。
4. 日历控件初始化
在引用日历控件时,经常出现日历控件无法使用。而我遇到该问题的原因是,日历控件未初始化。初始化日历控件的方法如下:
1)确保引入日历控件的初始化js如:
function init(){
var startDate =
document.getElementById("startDate");
var startCal = document.getElementById("startCal");
setCalendar(startDate, startCal);
var endDate = document.getElementById("endDate");
var endCal = document.getElementById("endCal");
setCalendar(endDate, endCal);
}
2)确保在body中初始化日历控件。
<body οnlοad="init()">
5. JS关闭当前页面
function windowclose(){
window.open("","_self");
top.opener=null;
top.close();
}
6. 自动转向另一页面
1)跳转后无法返回
<script type="text/javascript">
window.location="prj.action?operator=prj";
</script>
2)跳转后可以返回返回
<script type="text/javascript">
window.location.replace("/prj.action?operator=prj");
</script>
3)另一种自动转向
<%
if (request.getAttribute("T_flag") == null) {
response.sendRedirect("prj.action?operator=prj");
}
%>
在action中需加入
request.setAttribute("T_flag", "123"); //首页自动跳转,返回时的处理。