1

这我的网络电子邮件表单上有四个下拉菜单,如下所示

<select  name="newstaff"  class="formStyle" style="width:335px;" >
<option value="N/A">Please Select</option>
---- other options ---
<select name="allstaff"  class="formStyle" style="width:335px;" >
<option value="N/A">Please Select</option>
 ---- other options ---
<select  name="learning"  class="formStyle" style="width:400px;" >
<option value="N/A">Please Select</option>
---- other options ---
<select  name="leaders"  class="formStyle" style="width:335px;" >
<option value="N/A">Please Select</option>
---- other options ---

如果用户尝试提交此表单而不从至少一个下拉菜单中选择至少一个其他选项,我将如何编写一个会发出警报的验证函数“N/A

谢谢

编辑:

用于验证表单的代码:

function validateForm(objForm)
{
    var returnStatus = 1;

    if (objForm.newstaff.selectedIndex == 0) {
        alert("please make a selection!");
        returnStatus = 0;
    };
    if (returnStatus) {
        objForm.submit();
    }
}
4

1 回答 1

1

你很亲密。这里的区别是获取标签名称为 的元素数组select

validateForm = function(objForm) {
    var i = 0,
        valid = false,
        elements = objForm.getElementsByTagName("select");

    for ( ; i < elements.length; i++) {
        if (elements[i].selectedIndex) {
            valid = true;
            break;
        }
    }

    if (valid === true) {
        objForm.submit();
    } else {
        alert ("Please make a selection!");
    }
}
于 2012-11-30T13:15:51.917 回答