HTML部分

<!DOCTYPE html>
<html> <head>
<title></title> </head> <body>
<form name="form1" id="form1" >
<div>
<ul>
<li><input type="radio" name="color" value="red" />Red</li>
<li><input type="radio" name="color" value="green" />Green</li>
<li><input type="radio" name="color" value="blue" />Blue</li>
</ul>
<ul>
<li>
<!-- type='select-one' -->
<select name="selectbox" id='selectbox'>
<options>
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
</options>
</select>
</li>
</ul>
<ul>
<li>
<!-- type='multiple' -->
<select multiple id="selectbox2">
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
</select>
</li>
</ul>
<input type="text" pattern="\d+" size='25' maxlength="50" value="initial value" name="textbox1">
<textarea cols="5" rows="5">initial value</textarea>
<input type="time">
<input type="email" />
<input type="url" />
<input type="number" />
<input type="submit" value="Submit Form" name="submit-btn" />
<input type="reset" value="reset Form" />
<!-- <button type="button" value="OK">OK</button>
<button type="submit" value="OK">Submit Form</button> -->
</div>
<div contenteditable id="richedit" style="height: 300px;"></div>
<iframe name="richedit" style="height: 100px;" src="blank.htm"></iframe>
</form> <script type="text/javascript" src="form.js" ></script>
</body> </html>

Form.js

//在HTML中,表单是由<form>元素来表示的,在javascript中,表单对应的是HTMLFormElement 继承自HTMLElement
//HTMLElement有下列独有的属性和方法。
//acceptCharset 服务器能够处理的字符集,等价于HTML中accept-Charset特性
//action 接收请求的URL,等价于HTML中的action特性
//elements 表单中所有控件的集合(HTMLCollection).
//enctype 请求编码类型,等价于HTML中的enctype特性
//length 表单中的控件数量
//method 要发送HTPP请求类型,通常是GET或POST,等价于HTML中的method特性
//name 表单的名称,等价于HTML的name特性
//submit 提交表单
//target 用于发送请求或者接收响应的窗口名称,等价于HTML的target特性
// document.getElementById("form1") document.forms - document.forms[0] document.forms["form1"] var form = document.getElementById("form1"); form.addEventListener('submit', function (event) { //避免表单多次提交
var btn = (event ? event : window.event).target.elements['submit-btn'];
btn.disabled = true; //验证等操作
var colorFields = form.elements['color'];
alert(colorFields.length);
var firstColorFiled = colorFields[0];
var firstFormFiled = form.elements[0];
alert(firstColorFiled == firstFormFiled); if (event.preventDefault) {
event.preventDefault();
}
else {
event.returnValue = false;
} }, false); form.addEventListener('reset', function (event) {
if (event.preventDefault()) {
event.preventDefault();
}
else {
event.returnValue = false;
}
}, false); //form.submit(); 提交表单 form.reset(); 重置表单 //14.1.3 表单字段
var colorFields = form.elements['color'];
// alert(colorFields.length);
var firstColorFiled = colorFields[0];
var firstFormFiled = form.elements[0];
//alert(firstColorFiled == firstFormFiled); //表单字段共有属性
//disabled boolean 当前字段是否被禁用
//form 只想当前字段所属表单的指针,只读
//name 当前字段的名称
//readOnly boolean 表示改字段是否是只读
//tabIndex 表是当前字段的切换tab 序号
//type 当前字段的类型,如 checkbox radio 等
//value 当前字段将提交给服务器的值 var field = form.elements[0]; field.value = 'Another value';
field.focus();
field.disabled = true;
field.type = 'checkbox';
// alert(form.form ==form); //14.2.1 选择文本
var textbox = document.forms[0].elements['textbox1'];
// textbox.select();
// textbox.addEventListener('focus',function(event){
// (event?event:window.event).target.select();
// },false); //选择事件
// textbox.addEventListener('select',function(event){
// //alert('Text selected:'+textbox.value);
// //取得选择的文本
// alert(textbox.value.substring(textbox.selectionStart,textbox.selectionEnd));
// },false); //选择部分文本
textbox.value = 'Hello world!';
textbox.focus(); //获得焦点才生效
//textbox.setSelectionRange(0,textbox.value.length);
textbox.setSelectionRange(0, 3); //谷歌浏览器支持
// textbox.setSelectionRange(4,7); //选择所有文本 IE浏览器支持
// var range = textbox.createTextRange();
// range.collapse(true);
// range.moveStart('character',0);
// range.moveEnd('character',textbox.value.length);
// range.select(); //14.2.2 过滤输入 textbox.addEventListener('keypress', function (event) { //var target = event?event.target:window.event.target;
var charCode = null;
if (typeof event.charCode == 'number') {
charCode = event.charCode;
} else {
charCode = event.keyCode;
}
//非数字阻止继续冒泡执行
if (!/\d/.test(String.fromCharCode(charCode)) && charCode > 9 && !event.ctrlKey) {
(event ? event : window.event).preventDefault();
} }, false); //阻止复制的内容不合规
textbox.addEventListener('paste', function (event) {
var text = (event.clipboardData || window.ClipboardData).getData('text');
if(!/^\d*$/.test(text)){
(event?event:window.event).preventDefault();
}
}, false); //14.2.4 HTML5约束验证API //<input type='text' name='username' required /> 必填字段 var isRequiredSupported = 'required' in document.createElement('input'); //检验浏览器是否支持required属性 //检验字段有效性
if(document.forms[0].checkValidity()){
//表单有效
} form.noValidate =true;//禁用验证 // 14.3 选择框脚本 //select 方法
//add(newOption,relOption) 向控件插入新元素,其位置在相关项relOption
//multiple 布尔值 表示是否允许多选项选择
//options 控件中所有<option>元素的HTLCollection
//remove(index) 移除指定位置的项
//selectedIndex 基于0的选中项的索引,如果没有选中项 则值为-1. 对于多选的控件,只保持选中项的第一项索引
//size 选择框中可见的行数 //DOM中每个option的元素都有一个HTMLOptionElement 对象表示,有如下属性
//index 当前选项在options集合中的索引
//label 当前选择项的标签
//selected 布尔值,表示当前项是否被选中
//text 选项的文本
//value 选项的值 //14.3.1 选择选项
var selectbox = document.getElementById("selectbox");
var selectedOption = selectbox.options[selectbox.selectedIndex];
selectbox.options[1].selected =true; //14.3.2 添加选项
var newOption = document.createElement('option');
newOption.appendChild(document.createTextNode('Option Text'));
newOption.setAttribute('value','Option value');
selectbox.appendChild(newOption); //14.3.3 移除选项
// selectbox.removeChild(selectbox.options[0]);
// selectbox.remove(0);
selectbox.options[0] =null; //14.3.4 移动和重排选项
var selectbox2 = document.getElementById('selectbox2');
selectbox2.appendChild(selectbox.options[0]); //selectbox.option[0] 添加到第二select的时候会从第一个select 移除 var optionToMove = selectbox.options[1];
selectbox.insertBefore(optionToMove,selectbox.options[optionToMove.index-1]); //14.4 表单序列化 //14.5 富文本编辑 frames['richedit'].document.designMode ='on';
frames["richedit"].document.execCommand('bold',false,null);
frames['richedit'].document.execCommand('italic',false,null);
frames['richedit'].document.execCommand('createlink',false,'http://www.wrox.com');
frames['richedit'].document.execCommand('formateblock',false,'<h1>');

  

javascript权威指南第14章 表单脚本示例代码的更多相关文章

  1. JavaScript高级程序设计第14章表单脚本 (学习笔记)

    第十四章 表单脚本 1.阻止默认表单提交 1.提交表单数据 1.使用type=submit提交按钮 2.使用submit():方法 注意:当用户点击提交按钮时,会触发submit事件,从而在这里我们有 ...

  2. 【笔记】javascript权威指南-第三章-类型,值和变量

    javascript中的原始类型和对象类型(基本类型和引用类型) //本书是指:javascript权威指南    //以下内容摘记时间为:2013.7.27   计算机程序运行时需要对值(value ...

  3. 【笔记】javascript权威指南-第六章-对象

    对象 //本书是指:javascript权威指南    //以下内容摘记时间为:2013.7.28 对象的定义: 1.对象是一种复合值:将很多值(原始值或者对象)聚合在一起,可以通过名字访问这些值. ...

  4. javascript权威指南第11章 DOM扩展

    //javascript 权威指南 第三版 第11章 DOM扩展 //取得body元素 var body = document.querySelector("body"); //取 ...

  5. JavaScript基础笔记(十)表单脚本

    表单脚本 一.表单基础知识 JavaScript中表单对应的是HTMLFormElement类型,该类型继承自HTMLElement类型. 通过document.forms可以获得所有表单元素,通过数 ...

  6. javascript权威指南第13章 事件示例代码

    html 部分 <!DOCTYPE html> <html> <head> <title>Event Bubling Example</title ...

  7. javascript权威指南第21章 Ajax和Comet

    function createXHR(){ if(typeof XMLHttpRequest !='undefined'){ return new XMLHttpRequest(); }else if ...

  8. 《JavaScript高级程序设计》笔记:表单脚本(十四)

    表单的基础知识 在HTML中,表单是由<form>元素来表示的,而在JS中,表单对应的是HTMLFormElement类型.HTMLFormElement继承了HTMLElement,因而 ...

  9. ANTLR4权威指南 - 第7章 通过特定应用程序代码解耦语法

    第7章 通过特定应用程序代码解耦语法 到目前为止,我们已经知道了怎么用ANTLR的语法来定义语言了,接下来我们要给我们的语法注入一些新的元素了.就语法本身而言,其用处并不大,因为它只能告诉我们一个用户 ...

随机推荐

  1. SSM-CRUD

    一.项目介绍 前端技术:query+Bootstrap+ajax+json 后端技术:SSM(spring.springMVC.mybatis).JSR303校验 数据库:mysql 服务器:tomc ...

  2. PAT(B) 1092 最好吃的月饼(C)统计

    题目链接:1092 最好吃的月饼 (20 point(s)) 题目描述 月饼是久负盛名的中国传统糕点之一,自唐朝以来,已经发展出几百品种. 若想评比出一种"最好吃"的月饼,那势必在 ...

  3. CentOS7+Docker+MangoDB下部署简单的MongoDB分片集群

    简单的在Docker上快速部署MongoDB分片集群 前言 文中使用的环境如下 OS:CentOS Linux release 7.5.1804 (Core) Docker:Docker versio ...

  4. Manjaro 使用基础

    一.pacman/yay 的基础命令 Manjaro 装好后,需要运行的第一条命令: sudo pacman -Syy ## 强制更新包数据 sudo pacman-mirrors --interac ...

  5. netty--使用注意事项

  6. 递推问题 hdu 2046 与1143的比对

    2046 在2×n的一个长方形方格中,用一个1× 2的骨牌铺满方格,输入n ,输出铺放方案的总数.例如n=3时,为2× 3方格,骨牌的铺放方案有三种,如下图:   Input 输入数据由多行组成,每行 ...

  7. hdu 2822 ~!!!!!!坑死我

    首先 在此哀悼...  为我逝去的时间哀悼...  每一步都确定再去写下一步吧...日狗 不过还是有点收获的..  对优先队列的使用 有了进一步的理解 先上代码 #include<iostrea ...

  8. Android笔记(十六) 简易计算器

    实现功能: 简单计算器 布局及美化 采用LinearLayout嵌套LinearLayout实现布局. 要求 1. 按钮所有文字居于右下角 2. 按钮为白色,点击变成橘色 3. 显示屏文字居右显示并且 ...

  9. Python基础笔记一

    1. 分片的步长,默认为值1,表示为 xx[s:t:v] ----从索引s到索引t,每隔v,取对应索引位置的值 xx = 'hello,world' #从索引0-10,共11个字符 xx[2:] #从 ...

  10. Linux系统用终端打开图片

    一.现在开发多数使用的系统都是linux系统,但有的时候会遇到一些比较麻烦的小问题,比如:在某个文件夹中存入大量的图片时,想要查看某张图片的时候,当你使用图形化显示的时候,就会很卡,所以在这里我针对于 ...