js使用DOM设置单选按钮、复选框及下拉菜单的方法

前端之家收集整理的这篇文章主要介绍了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; }

dio" name="camera" id="canon" value="Canon">

单选按钮在表单中即它是一组供用户选择的对象,但每次只能选一个。每一个都有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 changeBoxes(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; }

Box" name="hobby" id="ball" value="ball">

复选框原理利用checked属性布尔值进行确定,全选和不全选可以采用0和1的方式传递参数。

3.下拉菜单

下拉菜单功能一样,当下拉菜单为多选时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">length502" 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; 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">selected502" 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; 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">SelectedIndex502" 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">text502" 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">value502" 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">type502" 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">options502" 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 = oSelectBox.selectedIndex; //获取选中项 alert("您选中了" + oSelectBox.options[iChoice].text); }

②. 下拉菜单为多选时,取值

代码如下:
Box = oForm.constellation; var aChoices = new Array(); //遍历整个下拉菜单 for (var i = 0; i < oSelectBox.options.length; i++) if (oSelectBox.options[i].selected) //如果被选中 aChoices.push(oSelectBox.options[i].text); //压入到数组中 alert("您选了:" + aChoices.join()); //输出结果 }

③. 通用取值(下拉单选和多选的情况)

代码如下:
Box) { var oForm = document.forms["myForm1"]; var oSelectBox = oForm.elements[Box]; //根据参数相应的选择下拉菜单 if (oSelectBox.type == "select-one") { //判断是单选还是多选 var iChoice = oSelectBox.selectedIndex; //获取选中项 alert("单选,您选中了" + oSelectBox.options[iChoice].text); } else { var aChoices = new Array(); //遍历整个下拉菜单 for (var i = 0; i < oSelectBox.options.length; i++) if (oSelectBox.options[i].selected) //如果被选中 aChoices.push(oSelectBox.options[i].text); //压入到数组中 alert("多选,您选了:" + aChoices.join()); //输出结果 } }

希望本文所述对大家的javascript程序设计有所帮助。

原文链接:https://www.f2er.com/js/56189.html

猜你在找的JavaScript相关文章