当前位置: 首页 > 图文教程 > 网络编程 > Javascript > JS checkbox控制操作代码

Javascript
form中限制文本字节数js代码
use jscript with List Proxy Server Information
use jscript List Installed Software
List Installed Software Features
List Information About the Binary Files Used by an Application
List the Codec Files on a Computer
List the UTC Time on a Computer
List Installed Hot Fixes
excel操作之Add Data to a Spreadsheet Cell
Add Formatted Data to a Spreadsheet
Apply an AutoFormat to an Excel Spreadsheet
JavaScript语法着色引擎(demo及打包文件下载)
类之Prototype.js学习
一款JavaScript压缩工具:X2JSCompactor
iis6+javascript Add an Extension File
jscript之Open an Excel Spreadsheet
jscript之Read an Excel Spreadsheet
jscript之List Excel Color Values
去除图像或链接黑眼圈的两种方法总结
Add a Formatted Table to a Word Document

Javascript 中的 JS checkbox控制操作代码


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 341 ::
收藏到网摘: n/a

下面的有关实现chckbox全选的方法或多或少存在一些缺陷,具体的方法在另一个帖子中有详细的说明
点击运行可以看到效果:
[Ctrl+A 全选 提示:你可先修改部分代码,再按运行]

例二:
要实现的功能如图所示:
全选的checkbox的js代码如下:
var status=true;
function allselect(){
var tags=document.getElementsByTagName("input");
for (i = 0; i < tags.length; i++)
{
if (tags[i].type == "checkbox")
{
tags[i].checked=status;
}
}
status=!status;
}
当点击显示职位,申请选中职位,放入收藏夹时的js代码如下:
function panduan(){
var gou=0;
var tags=document.getElementsByTagName("input");
for (i = 0; i < tags.length; i++)
{
if (tags[i].type == "checkbox")
{
if(tags[i].id!="Checkbox1"){ //这里的Checkbox1为全选checkbox
if(tags[i].checked==true){
gou++;
}
}
}
}
if(gou==0){
window.alert("请要在选择的职位前打勾!");
return false;
}
}
例三:
点击运行可以看到效果:
[Ctrl+A 全选 提示:你可先修改部分代码,再按运行]

顺便看看getElementsByTagName的用法
Javascript为我们提供了两种获取document引用的方式:getElementById和getElementsByTagName.前者返回一个指向具有指定Id属性的元素引用,而后者则返回具有该标签的元素数组,前者应用较多,也是比较熟悉的,本文针对后者做简要解释。
比方说:定义一个table,其中有多个td标签,现在需要更改第二个单元格的背景颜色,则可用getElementsByTagName.具体代码如下:
......
<table style="width:100%;">
<tr><td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td>8</td>
<td>9</td>
</tr>
</table>
......
var tdObject=document.getElementsByTagName("td").item(1);
tdObject.style.backgroundColor="blue";
......