Html表单使用实例
原文
https://www.jianshu.com/p/b01f32844ac1
大纲
1、单选框多选框实现的商品选择
2、添加下拉框和删除下拉框
3、观察textarea中事件处理器的运行顺序
推荐
1、单选框多选框实现的商品选择
<html>
<head>
<title>Test</title>
<script>
var radCpuSpeedIndex = 0;
function radCPUSpeed_onclick(radIndex) {
var returnValue = true;
if (radIndex == 1) {
returnValue = false;
alert("Sorry that processor speed is currently unavailable");
/**
Next line works around a bug in IE that doesn't cancel
the Default action properly
*/
document.form1.radCPUSpeed[radCpuSpeedIndex].checked = true;
} else {
radCpuSpeedIndex = radIndex;
}
return returnValue;
} function butCheck_onclick() {
var controlIndex;
var element;
var numberOfControls = document.form1.length;
var compSpec = "Your chosen processor speed is ";
compSpec =
compSpec + document.form1.radCPUSpeed[radCpuSpeedIndex].value;
compSpec =
compSpec + "\nWith the following additional components\n";
for (controlIndex = 0; controlIndex < numberOfControls; controlIndex++) {
element = document.form1[controlIndex];
if (element.type == "checkbox") {
if (element.checked == true) {
compSpec = compSpec + element.value + "\n";
}
}
}
alert(compSpec);
}
</script>
</head>
<body>
<form name="form1">
<p>Tick all of the components you want included on your computer </p>
<br>
<br>
<table>
<tr>
<td>DVD-ROM</td>
<td>
<input type="checkbox" name="cnkDVD" value="DVD-ROM">
</td>
</tr>
<tr>
<td>CD-ROM</td>
<td>
<input type="checkbox" name="cnkCD" value="CD-ROM">
</td>
</tr>
<tr>
<td>ZIP-ROM</td>
<td>
<input type="checkbox" name="cnkZIP" value="ZIP DRIVE">
</td>
</tr>
</table>
<p>Select the processor speed you require </p>
<br>
<br>
<table>
<tr>
<td>
<input type="radio" name="radCPUSpeed" checked onclick="return radCPUSpeed_onclick(0)" value="3.8 GHz">
</td>
<td>3.8 GHz</td>
</tr>
<tr>
<td>
<input type="radio" name="radCPUSpeed" checked onclick="return radCPUSpeed_onclick(1)" value="4.8 GHz">
</td>
<td>4.8 GHz</td>
</tr>
<tr>
<td>
<input type="radio" name="radCPUSpeed" checked onclick="return radCPUSpeed_onclick(2)" value="6 GHz">
</td>
<td>6 GHz</td>
</tr>
</table>
<input type="button" value="Check Form" name="butCheck" onclick="return butCheck_onclick()">
</form>
</body>
</html>
2、添加下拉框和删除下拉框
<html>
<head>
<title>Test</title>
<script>
function butRemoveWeb_onclick(){
if(document.form1.theDay.options[2].text == "Wednesday"){
document.form1.theDay.options[2] = null;
}else{
alert("There is no Wednesday here!");
}
console.log(document.form1.theDay.selectedIndex);
}
function butAddWed_onclick(){
if(document.form1.theDay.options[2].text != "Wednesday"){
var indexCounter ;
var days = document.form1.theDay;
var lastoption = new Option();
days.options[6] = lastoption;
for(indexCounter = 6;indexCounter >2; indexCounter--){
days.options[indexCounter].text = days.options[indexCounter -1].text;
days.options[indexCounter].value = days.options[indexCounter -1].value;
}
var option = new Option("Wednesday",2);
days.options[2] = option;
}
else{
alert("Do you want to have TWO Wednesday????");
}
console.log(document.form1.theDay.selectedIndex);
}
</script>
</head>
<body>
<form name="form1">
<select name=theDay size=5>
<option value=0 selected>Monday
<option value=1>Tuesday
<option value=2>Wednesday
<option value=3>Thursday
<option value=4>Friday
<option value=5>Saturday
<option value=6>Sunday
</select>
<br>
<input type="button" value="Remove Wednesday" name=butRemoveWeb onclick="butRemoveWeb_onclick()">
<input type="button" value="Add Wednesday" name=butAddWeb onclick="butAddWed_onclick()">
</form>
</body>
</html>
3、观察textarea中事件处理器的运行顺序
<html>
<head>
<title>Test</title>
<script>
//观察textarea中事件处理器的运行顺序
function DisplayEvent(eventName){
var myMessage = window.document.form1.textarea2.value;
myMessage = myMessage + eventName;
window.document.form1.textarea2.value = myMessage;
}
</script>
</head>
<body>
<form name="form1">
<textarea rows=15 cols=40 name=textarea1
onchange = "DisplayEvent('onchagen\n');"
onkeydown = "DisplayEvent('onkeydown\n');"
onkeypress = "DisplayEvent('onkeypress\n');"
onkeyup = "DisplayEvent('onkeyup\n\n');"
></textarea>
<textarea rows=15 cols=40 name=textarea2></textarea>
<br><br>
<input type="button" value="Clear Event TextArea"
name=button1 onclick="window.document.form1.textarea2.value=''"
>
</form>
</body>
</html>
推荐
在这里推荐两篇同样是我写的博客,一篇是详细的关于表单以及表单元素的知识点的文章Html表单元素及表单元素详解,还有一篇是我使用表单的过程中遇到的一些问题以及相应解释Html表单中遇到的问题,同样希望能对读者有所帮助。
Html表单使用实例的更多相关文章
- Bootstrap+PHP表单验证实例
简单实用的Bootstrap+PHP表单验证实例,非常适合初学者及js不熟悉者,还有ajax远程验证 js验证表单 1 $(document).ready(function() { 2 $('#def ...
- jQuery-easyui和validate表单验证实例
jQuery EasyUI 表单 - 表单验证插件validatebox 使用时需要向页面引入两个css文件如下: <link rel="stylesheet" href=& ...
- 034——VUE中表单控件处理之使用vue控制radio表单的实例操作
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- jquery-4 完整表单验证实例
jquery-4 完整表单验证实例 一.总结 一句话总结:在form的jquery对象中返回false即可终止表单提交. 1.验证的显示错误消息如何布局? 开始时隐藏,出现错误后显示 10 .erro ...
- React 表单元素实例
代码实例: <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset=" ...
- HTML5 web Form表单验证实例
HTML5 web Form 的开发实例! index.html <!DOCTYPE html> <html> <head> <meta charset=&q ...
- 纯原生javascript下拉框表单美化实例教程
html的表单有很强大的功能,在web早期的时候,表单是页面向服务器发起通信的主要渠道.但有些表单元素的样式没办法通过添加css样式来达到满意的效果,而且不同的浏览器之间设置的样式还存在兼容问题,比如 ...
- JavaScript表单验证实例
1. 长度限制<script>function test(){if(document.a.b.value.length>50){alert("不能超过50个字符!" ...
- Yii 框架表单验证---实例
- angular表单验证实例----可用的代码
前段时间,公司做一个单页面,就是一个表单验证,早开始在菜鸟教程上关注了angular,所以下派上用场了 angular里面对于表单验证,设置了很多指令. 也就是说不用自己写一些逻辑,直接绑定指令就行. ...
随机推荐
- git把本地文件上传到github上的步骤
1.清除clean 2.返回上一级cd .. 3.克隆仓库地址git clone+地址 4.添加忽悠文件vim .gitignore 5查看cat .gitignore 6.进入到test,并且添加所 ...
- 数组&对象
一.遍历数组的几种方式 var arr = [1,2,3]; Array.prototype.test=function(){} arr.name='jq' 1. for /* * inde ...
- [原创]react-vio-form 快速构建React表单应用
react-vio-form 是一个react的快速轻量表单库,能快速实现表单构建.提供自定义表单格式.表单校验.表单信息反馈.表单信息隔离等功能.可采用组件声明或者API的形式来实现表单的功能 de ...
- 10. LCD驱动程序 ——框架分析
引言: 由LCD的硬件原理及操作(可参看韦哥博客:第017课 LCD原理详解及裸机程序分析) 我们知道只要LCD控制器的相关寄存器正确配置好,就可以在LCD面板上显示framebuffer中的内容. ...
- SVN和Git代码管理小结
SVN和Git代码管理小结 之前,先用的是SVN,后来用了Git,最近又在用SVN. 关于代码管理,写几句. 由于自己参与的项目,人通常不超过10个人,版本不是很多,协作比较正常,感觉SVN ...
- PatentTips - Method to manage memory in a platform with virtual machines
BACKGROUND INFORMATION Various mechanisms exist for managing memory in a virtual machine environment ...
- ecshop微信接口基础认识
ecshop微信接口基础认识,当你要学习ecshop和微信整合的时候,你就必须研究ecshop的数据结构对接以及微信数据接口的基本知识.我们知道微信其实就是通过有效的消息推送,用JSON格式的数据或者 ...
- Apache/Tomcat/JBOSS/Nginx区别(转)
先说Apache和Tomcat的区别: Apache是世界使用排名第一的Web服务器软件.它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一. ...
- vc 常用语句
1) CMainFrame* pmainframe=(CMainFrame*)AfxGetMainWnd();CChildFrame *m_finderframe=(CChildFrame*)pmai ...
- JS概述
从Asp.NET跨越到JavaScript.这既是一个新的领域也是一个非常熟悉的地方,新是由于不知道什么是JavaScript,首先来了解一下什么是JavaScript. ...