js使用DOM设置单选按钮、复选框及下拉菜单的方法
本文实例讲述了js使用DOM设置单选按钮、复选框及下拉菜单的方法。分享给大家供大家参考。具体实现方法如下:
1.设置单选按钮
单选按钮在表单中即<input type="radio" />它是一组供用户选择的对象,但每次只能选一个。每一个都有checked属性,当一项选择为ture时,其它的都变为false.
先贴上一个例子:
<script type="text/javascript">
function getChoice() {
var oForm = document.forms["uForm1"];
var aChoices = oForm.camera;
for (i = 0; i < aChoices.length; i++) //遍历整个单选项表
if (aChoices[i].checked) //如果发现了被选中项则退出
break;
alert("相机品牌是:" + aChoices[i].value);
}
function setChoice(iNum) {
var oForm = document.forms["uForm1"];
oForm.camera[iNum].checked = true;
}
</script>
<form method="post" name="uForm1" action="addInfo.aspx">
相机品牌:
<p>
<input type="radio" name="camera" id="canon" value="Canon">
<label for="canon">Canon</label>
</p>
<p>
<input type="radio" name="camera" id="nikon" value="Nikon">
<label for="nikon">Nikon</label>
</p>
<p>
<input type="radio" name="camera" id="sony" value="Sony" checked>
<label for="sony">Sony</label>
</p>
<p>
<input type="radio" name="camera" id="olympus" value="Olympus">
<label for="olympus">Olympus</label>
</p>
<p>
<input type="radio" name="camera" id="samsung" value="Samsung">
<label for="samsung">Samsung</label>
</p>
<p>
<input type="radio" name="camera" id="pentax" value="Pentax">
<label for="pentax">Pentax</label>
</p>
<p>
<input type="radio" name="camera" id="others" value="其它">
<label for="others">others</label>
</p>
<p>
<input type="submit" name="btnSubmit" id="btnSubmit" value="Submit" class="btn">
</p>
<p>
<input type="button" value="检测选中对象" onclick="getChoice();">
<input type="button" value="设置为Canon" onclick="setChoice(0);">
</p>
</form>
单选按钮在表单中即<input type="radio" />它是一组供用户选择的对象,但每次只能选一个。每一个都有checked属性,当一项选择为ture时,其它的都变为false.
从以上代码中看出,id和name是不同的,一组单选按钮中它们的name是相同的,只有一个被选中。id则是绑定<label>或者其它选择作用的。
其中代码中:检查被选中对象的代码是(当某一项的chcked值为ture时,遍历结束)
var oForm = document.forms["uForm1"];
var aChoices = oForm.camera;
for (i = 0; i < aChoices.length; i++) //遍历整个单选项表
if (aChoices[i].checked) //如果发现了被选中项则退出
break;
alert("相机品牌是:" + aChoices[i].value);
2.设置多选框
与单选按钮不同,复选框<input type="checkbox" />可以同时选中多个选项进行处理,邮箱中每条邮件之前的复选框就的典型的运用
<script type="text/javascript">
function checkbox() {
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;
}
</script>
<form method="post" name="myForm1" action="addInfo.aspx">
喜欢做的事:
<p>
<input type="checkbox" name="hobby" id="ball" value="ball">
<label for="ball">打球</label>
</p>
<p>
<input type="checkbox" name="hobby" id="TV" value="TV">
<label for="TV">看电视</label>
</p>
<p>
<input type="checkbox" name="hobby" id="net" value="net">
<label for="net">上网</label>
</p>
<p>
<input type="checkbox" name="hobby" id="book" value="book">
<label for="book">看书</label>
</p>
<p>
<input type="checkbox" name="hobby" id="trip" value="trip">
<label for="trip">旅游</label>
</p>
<p>
<input type="checkbox" name="hobby" id="music" value="music">
<label for="music">音乐</label>
</p>
<p>
<input type="checkbox" name="hobby" id="others" value="其它">
<label for="others">其它</label>
</p>
<p>
<input type="button" value="全选" onclick="changeBoxes(1);" />
<input type="button" value="全不选" onclick="changeBoxes(0);" />
<input type="button" value="反选" onclick="changeBoxes(-1);" />
<input type="button" value="提交" onclick="checkbox()" />
</p>
</form>
复选框原理利用checked属性布尔值进行确定,全选和不全选可以采用0和1的方式传递参数。
3.下拉菜单
下拉菜单<select>是比较常用的表单元素。当它的下拉为单选时,和单选按钮<input type="radio" />功能一样,当下拉菜单为多选时multiple="multiple时,功能相当复选框,但所占面积远小于复选框。
下拉菜单的常用属性:
属性 | 说明 |
length | 表示选项<option>个数 |
selected | 布尔值,表示<option>是否被选中 |
SelectedIndex | 被选中选项的序列号,如果没有选项被选中则为-1,对于多选下拉菜单而言,返回第一个被选中 的序号,从0开始计数 |
text | 选项的文本 |
value | 选项的值 |
type | 下拉菜单的类型,单选返回select-one,多选返回select-multiple |
options | 获取选项的数组 ,例如:oSelectBox.options[2],表示下拉菜单oSelectBox第三项 |
<script language="javascript">
function checkSingle() {
var oForm = document.forms["myForm1"];
var oSelectBox = oForm.constellation;
var iChoice = oSelectBox.selectedIndex; //获取选中项
alert("您选中了" + oSelectBox.options[iChoice].text);
}
</script>
<form method="post" name="myForm1">
<label for="constellation">星座:</label>
<p>
<select id="constellation" name="constellation" >
<option value="Aries" selected="selected">白羊</option>
<option value="Taurus">金牛</option>
<option value="Gemini">双子</option>
<option value="Cancer">巨蟹</option>
<option value="Leo">狮子</option>
<option value="Virgo">处女</option>
<option value="Libra">天秤</option>
<option value="Scorpio">天蝎</option>
<option value="Sagittarius">射手</option>
<option value="Capricorn">摩羯</option>
<option value="Aquarius">水瓶</option>
<option value="Pisces">双鱼</option>
</select>
</p>
<input type="button" onclick="checkSingle()" value="查看选项" />
</form>
<script type="text/javascript">
function checkMultiple() {
var oForm = document.forms["myForm1"];
var oSelectBox = 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()); //输出结果
}
</script>
<form method="post" name="myForm1">
<label for="constellation">星座:</label>
<p>
<select id="constellation" name="constellation" multiple="multiple" style="height:180px;">
<option value="Aries">白羊</option>
<option value="Taurus">金牛</option>
<option value="Gemini">双子</option>
<option value="Cancer">巨蟹</option>
<option value="Leo">狮子</option>
<option value="Virgo">处女</option>
<option value="Libra">天秤</option>
<option value="Scorpio">天蝎</option>
<option value="Sagittarius">射手</option>
<option value="Capricorn">摩羯</option>
<option value="Aquarius">水瓶</option>
<option value="Pisces">双鱼</option>
</select>
</p>
<input type="button" onclick="checkMultiple()" value="查看选项" />
</form>
<script language="javascript">
function getSelectValue(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()); //输出结果
}
}
</script>
<form method="post" name="myForm1">
星座:
<p>
<select id="constellation1" name="constellation1">
<option value="Aries" selected="selected">白羊</option>
<option value="Taurus">金牛</option>
<option value="Gemini">双子</option>
<option value="Cancer">巨蟹</option>
<option value="Leo">狮子</option>
<option value="Virgo">处女</option>
<option value="Libra">天秤</option>
<option value="Scorpio">天蝎</option>
<option value="Sagittarius">射手</option>
<option value="Capricorn">摩羯</option>
<option value="Aquarius">水瓶</option>
<option value="Pisces">双鱼</option>
</select>
<input type="button" onclick="getSelectValue('constellation1')" value="查看选项" />
</p>
<p>
<select id="constellation2" name="constellation2" multiple="multiple" style="height:120px;">
<option value="Aries">白羊</option>
<option value="Taurus">金牛</option>
<option value="Gemini">双子</option>
<option value="Cancer">巨蟹</option>
<option value="Leo">狮子</option>
<option value="Virgo">处女</option>
<option value="Libra">天秤</option>
<option value="Scorpio">天蝎</option>
<option value="Sagittarius">射手</option>
<option value="Capricorn">摩羯</option>
<option value="Aquarius">水瓶</option>
<option value="Pisces">双鱼</option>
</select>
<input type="button" onclick="getSelectValue('constellation2')" value="查看选项" />
</p>
</form>
希望本文所述对大家的javascript程序设计有所帮助。