选择使用javascript检查标记状态?
我有两个选择标签现在我想要的是,一次只能从两个选择标签中选择一个用户应该只选择一个,如果用户从两个标签中选择,那么它应该显示错误,即只能选择一个 我用的是什么选择使用javascript检查标记状态?,javascript,jquery,html,dom,Javascript,Jquery,Html,Dom,我有两个选择标签现在我想要的是,一次只能从两个选择标签中选择一个用户应该只选择一个,如果用户从两个标签中选择,那么它应该显示错误,即只能选择一个 我用的是什么 var $institution=document.getElementById('institutionselect').value; var $section=document.getElementById('sectionselect').value; if($institution.selectedIndex = 0 &
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;
}
请帮助更正代码,谢谢 您需要做的就是在一个条件下检查这两个值,如下所示:
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
问题是您正在分配而不是比较。使用
=
代替=
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');
如果($institution.selectedIndex==0&&$section.selectedIndex==0){
警报(“请从两个选项中选择一个”);
返回false;
}
};代码>
挑选
项目1
项目2
挑选
项目1
项目2
检查