使用JavaScript验证多个选择框不是空的
我正在尝试使用JavaScript验证多个选择框是否为空。下面是我在字段名中没有[]的所有其他输入框上使用的代码使用JavaScript验证多个选择框不是空的,javascript,html,Javascript,Html,我正在尝试使用JavaScript验证多个选择框是否为空。下面是我在字段名中没有[]的所有其他输入框上使用的代码 <script type="text/javascript"> function validateForm() { var x=document.forms["escalations"]["SupportGroup[]"].value; if (x==null || x=="" || x=="---< Select Delivery Team >---")
<script type="text/javascript">
function validateForm()
{
var x=document.forms["escalations"]["SupportGroup[]"].value;
if (x==null || x=="" || x=="---< Select Delivery Team >---")
{
alert("Please select a Support Group.");
return false;
}
}
</script>
它可以很好地用于单个输入,但当我为多个输入添加“[]”时,它会在选择或不选择某个选项时发出警报。有什么想法吗?谢谢
html代码是
<form name="escalations" onsubmit="return validateForm()" action="submitescalation.php?SM=SN" method="post" enctype="multipart/form-data">
<select id="s0" multiple="multiple" name="SupportGroup[]" onchange="GetCompany();GetTitle();GetContact();" style="height: 25px">
<option>Company1</option><option>Company2</option><option> Company3</option></select>
</form>
这将有助于选择第一个选项项的文本:
var x=document.forms["escalations"]["SupportGroup[] option:selected"].eq(0).text();
if (document.forms["escalations"]["SupportGroup[]"].selectedIndex >= 0) {
// an option other than the first has been selected
}
或单击此按钮以获取所选第一个选项项的值
var x=document.forms["escalations"]["SupportGroup[] option:selected"].eq(0).val();
也许是这样的:
function validateForm()
{
//var x=document.forms["escalations"]["SupportGroup"].value;
selects= document.getElementsByTagName('select');
for(i=0;i<selects.length;i++){
x=selects[i].value;
if (x==null || x=="" || x=="---< Select Delivery Team >---")
{
alert("Please select a Support Group.");
return false;
}
}
}
注意:如果我理解正确-您在页面上有多个选择框,名称相同,并且您将值作为数组发送到服务器端脚本?第一个选项的值可能为--