前端之家 收集整理的这篇文章主要介绍了
js使用DOM设置单选按钮、复选框及下拉菜单的方法 ,
前端之家 小编觉得挺不错的,现在分享给大家,也给大家做个参考。
本文实例讲述了js使用DOM设置单选按钮、复选框及下拉菜单 的方法 。分享 给大家供大家参考。具体实现方法 如下:
1.设置单选按钮
单选按钮在表单中即 它是一组供用户 选择的对象,但每次只能选一个。每一个都有checked属性 ,当一项选择为ture时,其它的都变为false.
先贴上一个例子:
退出
break;
alert("相机品牌是:" + aChoices[i].value);
}
function setChoice(iNum) {
var oForm = document.forms["uForm1"];
oForm.camera[iNum].checked = true;
}
单选按钮在表单中即 它是一组供用户 选择的对象,但每次只能选一个。每一个都有checked属性 ,当一项选择为ture时,其它的都变为false.
从以上代码 中看出,id和name是不同的,一组单选按钮中它们的name是相同的,只有一个被选中。id则是绑定或者其它选择作用的。
其中代码 中:检查被选中对象的代码 是(当某一项的chcked值为ture时,遍历结束)
退出
break;
alert("相机品牌是:" + aChoices[i].value);
2.设置多选框
与单选按钮不同,复选框 可以同时选中多个选项进行处理,邮箱中每条邮件 之前的复选框就的典型的运用
Box() {
var str = document.getElementsByName("hobby");
var objarray = str.length;
var chestr = "";
for (j = 0; j < objarray; j++) {
if (str[j].checked == true) {
chestr += str[j].value + ",";
}
}
if (chestr == "") {
alert("请先选择一个爱好~!");
} else {
alert("您先择的是:" + chestr);
}
}
function changeBox es(action) {
var oForm = document.forms["myForm1"];
var oCheckBox = oForm.hobby;
for (var i = 0; i < oCheckBox .length; i++) //遍历每一个选项
if (action < 0) //反选
oCheckBox [i].checked = !oCheckBox [i].checked;
else //action为1是则全选,为0时则全不选
oCheckBox [i].checked = action;
}
复选框原理利用checked属性 布尔值进行确定,全选和不全选可以采用0和1的方式传递参数。
下拉菜单 是比较常用的表单元素。当它的下拉为单选时,和单选按钮功能 一样,当下拉菜单 为多选时multiple="multiple时,功能 相当复选框,但所占面积远小于复选框。
菜单的常用属性 :
Box-sizing: border-Box ; border-bottom: silver 1px solid; border-left: silver 1px solid; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; width: 619px; border-collapse: collapse; font: 13px/20px Verdana,Arial,Helvetica,sans-serif; white-space: normal; letter-spacing: normal; height: 219px; color: rgb(75,75,75); border-top: silver 1px solid; border-right: silver 1px solid; word-spacing: 0px; -webkit-text-stroke-width: 0px">
Box-sizing: border-Box ">
Box-sizing: border-Box ">
Box-sizing: border-Box ; border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 3px; line-height: 1.5; padding-left: 3px; padding-right: 3px; border-collapse: collapse; font-family: Verdana,Arial; font-size: 12px; word-break: break-all; border-top: silver 1px solid; border-right: silver 1px solid; padding-top: 3px">属性 502" style="Box -sizing: border-Box ; border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 3px; line-height: 1.5; padding-left: 3px; padding-right: 3px; border-collapse: collapse; font-family: Verdana,Arial; font-size: 12px; word-break: break-all; border-top: silver 1px solid; border-right: silver 1px solid; padding-top: 3px">说明Box-sizing: border-Box ">
Box-sizing: border-Box ; border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 3px; line-height: 1.5; padding-left: 3px; padding-right: 3px; border-collapse: collapse; font-family: Verdana,Arial; font-size: 12px; word-break: break-all; border-top: silver 1px solid; border-right: silver 1px solid; padding-top: 3px">length 502" style="Box -sizing: border-Box ; border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 3px; line-height: 1.5; padding-left: 3px; padding-right: 3px; border-collapse: collapse; font-family: Verdana,Arial; font-size: 12px; word-break: break-all; border-top: silver 1px solid; border-right: silver 1px solid; padding-top: 3px">表示选项个数Box-sizing: border-Box ">
Box-sizing: border-Box ; border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 3px; line-height: 1.5; padding-left: 3px; padding-right: 3px; border-collapse: collapse; font-family: Verdana,Arial; font-size: 12px; word-break: break-all; border-top: silver 1px solid; border-right: silver 1px solid; padding-top: 3px">selected 502" style="Box -sizing: border-Box ; border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 3px; line-height: 1.5; padding-left: 3px; padding-right: 3px; border-collapse: collapse; font-family: Verdana,Arial; font-size: 12px; word-break: break-all; border-top: silver 1px solid; border-right: silver 1px solid; padding-top: 3px">布尔值,表示 是否被选中Box-sizing: border-Box ">
Box-sizing: border-Box ; border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 3px; line-height: 1.5; padding-left: 3px; padding-right: 3px; border-collapse: collapse; font-family: Verdana,Arial; font-size: 12px; word-break: break-all; border-top: silver 1px solid; border-right: silver 1px solid; padding-top: 3px">SelectedIndex 502" style="Box -sizing: border-Box ; border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 3px; line-height: 1.5; padding-left: 3px; padding-right: 3px; border-collapse: collapse; font-family: Verdana,Arial; font-size: 12px; word-break: break-all; border-top: silver 1px solid; border-right: silver 1px solid; padding-top: 3px">被选中选项的序列号,如果没有选项被选中则为-1,对于多选下拉菜单 而言,返回第一个被选中
的序号,从0开始计数Box-sizing: border-Box ">
Box-sizing: border-Box ; border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 3px; line-height: 1.5; padding-left: 3px; padding-right: 3px; border-collapse: collapse; font-family: Verdana,Arial; font-size: 12px; word-break: break-all; border-top: silver 1px solid; border-right: silver 1px solid; padding-top: 3px">text 502" style="Box -sizing: border-Box ; border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 3px; line-height: 1.5; padding-left: 3px; padding-right: 3px; border-collapse: collapse; font-family: Verdana,Arial; font-size: 12px; word-break: break-all; border-top: silver 1px solid; border-right: silver 1px solid; padding-top: 3px">选项的文本Box-sizing: border-Box ">
Box-sizing: border-Box ; border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 3px; line-height: 1.5; padding-left: 3px; padding-right: 3px; border-collapse: collapse; font-family: Verdana,Arial; font-size: 12px; word-break: break-all; border-top: silver 1px solid; border-right: silver 1px solid; padding-top: 3px">value 502" style="Box -sizing: border-Box ; border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 3px; line-height: 1.5; padding-left: 3px; padding-right: 3px; border-collapse: collapse; font-family: Verdana,Arial; font-size: 12px; word-break: break-all; border-top: silver 1px solid; border-right: silver 1px solid; padding-top: 3px">选项的值Box-sizing: border-Box ">
Box-sizing: border-Box ; border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 3px; line-height: 1.5; padding-left: 3px; padding-right: 3px; border-collapse: collapse; font-family: Verdana,Arial; font-size: 12px; word-break: break-all; border-top: silver 1px solid; border-right: silver 1px solid; padding-top: 3px">type 502" style="Box -sizing: border-Box ; border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 3px; line-height: 1.5; padding-left: 3px; padding-right: 3px; border-collapse: collapse; font-family: Verdana,Arial; font-size: 12px; word-break: break-all; border-top: silver 1px solid; border-right: silver 1px solid; padding-top: 3px">下拉菜单 的类型,单选返回select-one,多选返回select-multipleBox-sizing: border-Box ">
Box-sizing: border-Box ; border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 3px; line-height: 1.5; padding-left: 3px; padding-right: 3px; border-collapse: collapse; font-family: Verdana,Arial; font-size: 12px; word-break: break-all; border-top: silver 1px solid; border-right: silver 1px solid; padding-top: 3px">options 502" style="Box -sizing: border-Box ; border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 3px; line-height: 1.5; padding-left: 3px; padding-right: 3px; border-collapse: collapse; font-family: Verdana,Arial; font-size: 12px; word-break: break-all; border-top: silver 1px solid; border-right: silver 1px solid; padding-top: 3px">获取 选项的数组 ,例如:oSelectBox .options[2],表示下拉菜单 oSelectBox 第三项
①. 下拉
菜单 获取 单选值
Box = oForm.constellation;
var iChoice = oSelect
Box .selectedIndex; //
获取 选中项
alert("您选中了" + oSelect
Box .options[iChoice].text);
}
②. 下拉菜单 为多选时,取值
Box = oForm.constellation;
var aChoices = new Array();
//遍历整个下拉
菜单
for (var i = 0; i < oSelect
Box .options.length; i++)
if (oSelect
Box .options[i].selected) //如果被选中
aChoices.push(oSelect
Box .options[i].text); //压入到数组中
alert("您选了:" + aChoices.join()); //
输出 结果
}
③. 通用取值(下拉单选和多选的情况)
Box) {
var oForm = document.forms["myForm1"];
var oSelect
Box = oForm.elements[
Box ]; //根据参数相应的选择下拉
菜单
if (oSelect
Box .type == "select-one") { //判断是单选还是多选
var iChoice = oSelect
Box .selectedIndex; //
获取 选中项
alert("单选,您选中了" + oSelect
Box .options[iChoice].text);
} else {
var aChoices = new Array();
//遍历整个下拉
菜单
for (var i = 0; i < oSelect
Box .options.length; i++)
if (oSelect
Box .options[i].selected) //如果被选中
aChoices.push(oSelect
Box .options[i].text); //压入到数组中
alert("多选,您选了:" + aChoices.join()); //
输出 结果
}
}
希望本文所述对大家的javascript程序设计有所帮助。
原文链接:https://www.f2er.com/js/56189.html