验证是否在下拉列表中选择了选项的正确方法是什么?我有一些选择元素(字段集)被设置为不显示,而其他元素是可见的。我只想验证那些可见的,否则表单将不会提交。
例如:
<fieldset id="a1_1">
<select name="a1_1">
<option value="" selected="selected">Select Age</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
</fieldset>
<fieldset id="a1_2" style=" display: none;">
<select name="a1_2">
<option value="" selected="selected">Select Age</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
</fieldset>
<fieldset id="a2_1">
<select name="a2_1">
<option value="" selected="selected">Select Age</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
</fieldset>
<fieldset id="a2_2" style=" display: none;">
<select name="a2_2">
<option value="" selected="selected">Select Age</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
</fieldset>
我在没有任何运气的情况下尝试这样的事情:
var x = document.forms["x"]["a1_1"].value;
if (x == null || x == "")
{
alert("Age must be selected");
return false;
}