您现在的位置是:首页 > 电脑技术查询 > web开发

怎么判断下拉框是否为空

编辑:chaxungu时间:2022-10-02 23:20:33分类:web开发

如何判断下拉框是否为空?
if(form.select1.options[0].text== " "){
alert( "提示信息 ");
form.select1.focus();
return false;
}


为何无效,或者代码不对,请帮忙修改!


------解决方案--------------------
判断第一项内容为空:document.getElementById( "select1 ").options[0].text == " "
判断整个下拉框为空:document.getElementById( "select1 ").options.length == 0
------解决方案--------------------
document.getElementById( "select1 ").length == 0
------解决方案--------------------
var sel=document.getElementById( "select1 ");
if(sel.value== " "){
alert( "提示信息 ");
sel.focus();
return false;
}