0
    function validateForm()
    {
       var ddlTitle = document.getElementById('<%=ddlTitle.ClientID %>');
        if (ddlTitle.value.replace(/^\s+|\s+$/, '').length < 1)
                    {
                        alert("Title should not be blank.");
                        ddlTitle.focus();//not working in mozilla but fine in IE and chrome
                        return false;
                    }

    }
<asp:DropDownList ID="ddlTitle" runat="server" CssClass="csstextbox">
                    <asp:ListItem Value="">[Select]</asp:ListItem>
                    <asp:ListItem>Dr.</asp:ListItem>
                    <asp:ListItem>Dr.(Mrs.)</asp:ListItem>
                </asp:DropDownList>
    <asp:Button ID="btn" runat="server" CssClass="cssbutton" Text="Save" Width="60px"
                        OnClientClick="return validateForm();"  />

这是 HTML 源代码

ddlTitle = document.getElementById('contentPlaceHolderMain_ddlTitle');
function validateForm()
        {
            if (ddlTitle.value.replace(/^\s+|\s+$/, '').length < 1)
            {
                alert("Title should not be blank.");
                ddlTitle.focus();
                return false;
            }
         }

问题仅适用于下拉列表适用于文本框

4

1 回答 1

1

Focus() 必须设置焦点尝试使用键盘键向上/向下箭头并检查。

于 2012-06-04T13:02:27.420 回答