技术共享
技术共享 Technology sharing 当前位置:首页->技术共享->技术共享

1.更改表单的action值
<html>
<head>
<script type="text/javascript">
function changeAction()
{
var x=document.getElementById("myForm")
alert("Original action: " x.action)
x.action="/htmldom/index.asp"
alert("New action: " x.action)
x.submit()
}
</script>
</head>
<body>
<form id="myForm" action="/i/eg_smile.gif">
名称:<input type="text" value="米老鼠" />
<input type="button" onclick="changeAction()"
value="改变 action 属性并提交表单" />
</form>
</body>
</html>
2.选定以及不选定checkbox
<html>
<head>
<script type="text/javascript">
function check()
{
document.getElementById("myCheck").checked=true
}

function uncheck()
{
document.getElementById("myCheck").checked=false
}
</script>
</head>
<body>
<form>
<input type="checkbox" id="myCheck" />
<input type="button" onclick="check()" value="选定复选框" />
<input type="button" onclick="uncheck()" value="取消选定复选框" />
</form>
</body>
</html>
3.一个表单中的若干个checkbox
<html>
<head>
<script type="text/javascript">
function createOrder()
{
coffee=document.forms[0].coffee
txt=""
for (i=0;i<coffee.length; i)
{
if (coffee[i].checked)
{
txt=txt coffee[i].value " "
}
}
document.getElementById("order").value="您订购的咖啡带有: " txt
}
</script>
</head>
<body>
<p>你喜欢怎么喝咖啡?</p>
<form>
<input type="checkbox" name="coffee" value="奶油">加奶油<br />
<input type="checkbox" name="coffee" value="糖块">加糖块<br />
<br />
<input type="button" onclick="createOrder()" value="发送订单">
<br /><br />
<input type="text" id="order" size="50">
</form>
</body>
</html>
4.使用单选按钮中的value
<html>
<head>
<script type="text/javascript">
function check(browser)
  {
  document.getElementById("answer").value=browser
  }
</script>
</head>
<body>
<p>您喜欢哪款浏览器?</p>
<form>
<input type="radio" name="browser" onclick="check(this.value)" value="Internet Explorer">Internet Explorer<br />
<input type="radio" name="browser" onclick="check(this.value)" value="Firefox">Firefox<br />
<input type="radio" name="browser" onclick="check(this.value)" value="Netscape">Netscape<br />
<input type="radio" name="browser" onclick="check(this.value)" value="Opera">Opera<br />
<br />
您喜欢的浏览器是:<input type="text" id="answer" size="20">
</form>
</body>
</html>
5.表单中的下拉菜单
<html>
<head>
<script type="text/javascript">
function favBrowser()
{
var mylist=document.getElementById("myList")
document.getElementById("favorite").value=mylist.options[mylist.selectedIndex].text
}
</script>
</head>
<body>
<form>
请选择您喜欢的浏览器:
<select id="myList" onchange="favBrowser()">
  <option>Internet Explorer</option>
  <option>Netscape</option>
  <option>Opera</option>
</select>
<p>您喜欢的浏览器是:<input type="text" id="favorite" size="20"></p>
</form>
</body>
</html>
6.另一个下拉菜单
<html>
<head>
<script type="text/javascript">
function moveNumbers()
{
var no=document.getElementById("no")
var option=no.options[no.selectedIndex].text
var txt=document.getElementById("result").value
txt=txt option
document.getElementById("result").value=txt
}
</script>
</head>
<body>
<form>
请选择数字:<br />
<select id="no">
 <option>0</option>
 <option>1</option>
 <option>2</option>
 <option>3</option>
 <option>4</option>
 <option>5</option>
 <option>6</option>
 <option>7</option>
 <option>8</option>
 <option>9</option>
</select>
<input type="button" onclick="moveNumbers()" value="-->">
<input type="text" id="result" size="20">
</form>
</body>
7.为若干表单添加快捷键
<html>
<head>
<script type="text/javascript">
function access()
{
document.getElementById('myName').accessKey="n"
document.getElementById('myPwd').accessKey="p"
document.getElementById('ie').accessKey="i"
document.getElementById('fx').accessKey="f"
document.getElementById('myButton').accessKey="b"
}
</script>
</head>
<body onload="access()">
<form>
姓名:<input id="myName" type="text" />
<br />
密码:<input id="myPwd" type="password" />
<br /><br />
选择您喜欢的浏览器:
<br />
<input type="radio" name="browser" id="ie" value="Internet Explorer">Internet Explorer<br />
<input type="radio" name="browser" id="fx" value="Firefox">Firefox
<br /><br />
<input type="button" value="点击我!" id="myButton" />
</form>
<p>(请使用 Alt <i>accesskey</i> 为不同的表单字段赋予焦点。)
</p>
</body>
</html>




上一篇:css中display属性与float属性
下一篇:smarty生成静态网页