您的位置:首页 > 其它

表单文本框中文本的获取,还有当前时间的显示

2010-07-08 21:45 477 查看
1. 以下是获取表单文本框中的内容例子:

str[0]= document.all.cnlTest1.value; //用第一种方法获得文本框中输入的内容
str[1]= document.all["cnlTest2"].value; //用第二种方法获得文本框中输入的内容
str[2]=document.all.tags('form')[0].cnlTest3.value;//用第三种方法获得文本框中输入的内容

<mce:script language="javascript"><!--

function Value()
{
var str=new Array(3);
str[0]= document.all.cnlTest1.value; 				//用第一种方法获得文本框中输入的内容
str[1]= document.all["cnlTest2"].value;				//用第二种方法获得文本框中输入的内容
str[2]=document.all.tags('form')[0].cnlTest3.value;//用第三种方法获得文本框中输入的内容
with(document)										//访问document对象的属性值
{
write("姓名:"+str[0]+"<br>");
writeln("住址:"+str[1]+"<br>");
writeln("工号:"+str[2]+"<br>");
}
}
// --></mce:script>
</head>
<body>

<form action="" method="get" name="form1">
<p>   </p>
<p>姓名:
<input id="cnlTest1" name="cnlTest1" value="">
</p>
<p>
住址: <input id="cnlTest2" name="cnlTest2" value="">
</p>
<p>
工号: <input id="cnlTest3" name="cnlTest3" value="">
</p>
<p>
<input name="" type="Submit" onClick="Value()" value="显示信息">
</p>
</form>
</body>


2. 另外一种直接根据ID号去获取文本值:

var text = document.getElementById("password");
alert("password is " + text.value);

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'getObject.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css">
-->

</head>

<mce:script type="text/javascript"><!--
function show(){
//var text = form1.textfield.value;
var text = document.getElementById("password");
alert("password is " + text.value);
var text1 =document.all.text.value;
alert("text1 is " + text1);

}

function println(str) {
document.write(str);
}

// --></mce:script>

<body id="body">
<form id="form1" name="form1" method="post" action="">
<label>
姓名:
<input type="text" id="text" name="textfield" />
</label>
<p>
<label>
密码:
<input type="password" id="password" name="textfield2" />
</label>
<p>
<p>
<label>
<%
//内容:
//<input type="text" id="content" name="content" onChange="println(this.value)"/>
%>
</label>
<p>
<input type="submit" name="Submit" value="查看密码和姓名" onclick="show()" />
</p>
</form>
</body>
</html>


3. 显示系统当前时间:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'blurfocus.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css">
-->

</head>

<mce:script type="text/javascript"><!--
function show(str) {
if(str.length>4) {
alert("大于4");
} else {
alert("小于4");
}
}

function showTime() {
var date = new Date();
form1.name.value = date.toLocaleString();
}

function myFun() {
setInterval("showTime()",1000);
}

// --></mce:script>

<body onLoad="myFun()">
<form id="form1" name="form1">
sName: <input type="text" id="name" name="name" onBlur="show(this.value)"></input>
</form>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐