焦点();不是在mozilla工作,但在IE和Chrome中很好

时间:2012-06-04 12:47:56

标签: javascript asp.net cross-browser

    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;
            }
         }

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

1 个答案:

答案 0 :(得分:1)

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