我正在为我的表单构建验证。我已经用下面的代码覆盖了输入文本字段:
function validateForm_id()
{
var ctrl = document.forms["form1"]["id"];
var x=ctrl.value;
if (x==null || x=="") {
document.getElementById('id').style.backgroundColor="#FFD4D4";
document.getElementById("id").style.borderColor="#FF7A7A";
document.all.id.innerText = "password required";
我想从我的选择下拉框中验证值。值为:
- 选择标题 -
先生
太太
MS
我希望用户无法提交if - Title - 是框中的值。
如何在上述代码的if (x==null || x=="")
中引用下拉框的第一个值?这是我的下拉列表:
<select name="title" onKeydown="Javascript: if (event.keyCode==13) post();" onFocus="validation_title()" onBlur="return validateForm_title()" id="title">
<option>- Title -</option>
<option value="1">Mr</option>
<option value="2">Mrs</option>
<option value="3">Ms</option>
<option value="4">Other</option>
</select> <div align="center" id="title" style="font-size:small; color:#FF0000;"><script type="text/javascript">document.all.title.innerText = " ";</script></div>
答案 0 :(得分:1)
您可以使用selectedIndex
的{{1}}属性来检查是否从未选择过第一个索引。
<select>
答案 1 :(得分:0)
var select = document.getElementById('mySelectID');
alert(select.options[0].value); //first option's value
alert(select.value); // currently selected value
要获取选项的文字
alert(select.options[select.selectedIndex].text);
答案 2 :(得分:0)
var opts = yourSelectObj.options;
然后循环选项。