Home > Web Front-end > JS Tutorial > body text

JS获取下拉框显示值和判断单选按钮的方法_javascript技巧

WBOY
Release: 2016-05-16 15:50:55
Original
1226 people have browsed it

本文实例讲述了JS获取下拉框显示值和判断单选按钮的方法。分享给大家供大家参考。具体如下:

1.本人做过很多项目,都需要得到select组件显示的值。下面是我经常用到的方法:

Html源码如下:

<html><body>
<select id="province" name="province" >
<option value="1">北京</option>
<option value="2">上海</option>
<option value="3">江苏</option>
<option value="4">河北</option>
</select>
<br/>
<input type="text" id="provinceName" name="provinceName"/>
<input type="button" onClick="setTxt()" value="赋值"/>
</body></html>

Copy after login

JS函数如下:

function setTxt()
{
 var _selObj=document.getElementByIdx_xx('province');//取下拉框的元素
 var _selVal=_selObj[_selObj.selectedIndex].text;//取下拉框被选中的值
 document.getElementByIdx_xx('provinceName').value=_selVal;
}

Copy after login

2.本人也做过很多判断radio单选按钮是否被选中的情况,下面是我常用到得方法:

HTML页面源码如下:

<html><body>
<input type="radio" name="time" value="2009-05-20"/>2009-05-20
<input type="radio" name="time" value="2009-05-21"/>2009-05-21
<input type="radio" name="time" value="2009-05-22"/>2009-05-22
<input type="radio" name="time" value="2009-05-23"/>2009-05-23
<input type="button" value="判断是否选择" onclick="alert(judge())"/>
</body></html>

Copy after login

JS函数代码:

function judge()
{ var status =false;
 var _radObj=document.getElementsByName('time');
 for(var i =0;i<_radObj.length;i++){
  if(_radObj[i].checked){
  status=true;
  }
 }
 if(! status){
  alert('未选中时间!');
 }
 return status;
}

Copy after login

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

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!