使用javascript选择标签状态检查
Select tag status check with javascript?
我现在有两个选择标签我想要的是一次只能选择一个即从两个选择标签中用户只能选择一个如果用户从两个标签中都选择了那么它应该显示错误只有一个应该被选择
我在用什么
var $institution=document.getElementById('institutionselect').value;
var $section=document.getElementById('sectionselect').value;
if($institution.selectedIndex = 0 && $section.selectedIndex = 0){
alert('please select one amongst two');
return false;
}else if($institution.selectedIndex = null && $section.selectedIndex = null){
alert('please select one amongst two');
return false;
}
请帮忙更正代码谢谢!
问题是你在分配而不是比较。用==
代替=
。
if($institution.selectedIndex = 0 && $section.selectedIndex = 0)
也更新这行删除.value
以便使用.selectedIndex
:
var $institution=document.getElementById('institutionselect');
var $section=document.getElementById('sectionselect');
一个例子:
var check = function() {
var $institution = document.getElementById('institutionselect');
var $section = document.getElementById('sectionselect');
if ($institution.selectedIndex == 0 && $section.selectedIndex == 0) {
alert('please select one amongst two');
return false;
}
};
<select id='institutionselect'>
<option>Select</option>
<option>Item 1</option>
<option>Item 2</option>
</select>
<select id='sectionselect'>
<option>Select</option>
<option>Item 1</option>
<option>Item 2</option>
</select>
<button onclick="check();">Check</button>
您所需要做的就是在一个条件下检查两个值,如下所示:
var $institution = document.getElementById('institutionselect').value;
var $section = document.getElementById('sectionselect').value;
// if both variable have values
if ( $institution && $section ){
alert('please select one amongst two');
return;
}
// do something here
相关文章:
- 使用javascript反复检查用户在facebook上的登录状态
- 如何在渲染函数中检查 react 中子组件的状态
- 检查元素是否将状态从隐藏更改为可见
- 如何使用sendy检查用户(已订阅或未订阅)的状态
- 我如何为列出的选项卡元素编写一个Protractor测试,它会检查它是否's是否处于活动状态
- Angular+bootstrap ui,检查当前选项卡是否已处于活动状态
- Angular UI路由器-检查父状态
- 如何使用Javascript检查当前鼠标按钮状态
- 如何重置剑道MVVM单选按钮's已检查状态
- 如果设置了位置,如何在每个状态下检查路由器ui,如果没有,则转到登录页
- 检查网络驱动程序是否浏览器仍处于打开状态
- 检查 redux 状态是否更改的简单方法
- 检查复选框状态-Javascript
- 在检查单选按钮检查状态后启用/禁用表单元素
- 流星中的状态检查延迟
- 使用 javascript 实现状态检查的最佳方法
- 谷歌地理编码 API 状态检查 - OVER_QUERY_LIMIT
- 使用javascript选择标签状态检查
- 即时通讯在线状态检查/指示器为GTalk, MSN (Skype), Facetime(或消息)
- Internet Explorer在进行登录状态检查时强制执行304