简体   繁体   English

这个jquery验证码有什么问题?

[英]What is wrong with this jquery validation code?

The code is supposed to hide the textbox when the page loads and make it visible only when the user selects Other. 代码应该在页面加载时隐藏文本框,并仅在用户选择“其他”时才显示。

<script type="text/javascript">
        $(document).ready(function () {
        $('#ddlMajor').change(function () {
        if ($(this).val() == 'Other') {
                //                $('#txtOther').show();
         $('#txtOther').css('display', 'inline');
         }
        else {
                //                $('#txtOther').hide();
        $('#txtOther').css('display', 'block');
        }

        });

});
</script>

<asp:TextBox runat="server" ID="txtOther" style="display:none;" > </asp:TextBox>

<asp:DropDownList runat="server" ID="ddlMajor">
                 <asp:ListItem Value="Accounting">Accounting</asp:ListItem> 
                 <asp:ListItem Value="Management">Management</asp:ListItem>
                 <asp:ListItem Value="Other">Other</asp:ListItem>
</asp:DropDownList>

As you are using a Server Side control the ID will be re-rendered. 在使用服务器端控件时,将重新呈现ID。 You could put code blocks in your javascript, but I would recommend using a class instead: 你可以把代码块放在你的javascript中,但我建议你改用一个类:

<asp:TextBox runat="server" ID="txtOther" style="display:none;" CssClass="txtOther"> </asp:TextBox>

<asp:DropDownList runat="server" ID="ddlMajor" CssClass="ddlMajor">
                 <asp:ListItem Value="Accounting">Accounting</asp:ListItem> 
                 <asp:ListItem Value="Management">Management</asp:ListItem>
                 <asp:ListItem Value="Other">Other</asp:ListItem>
</asp:DropDownList>

 $('.ddlMajor').change(function () {
...
});

I also believe your CSS display values are incorrect. 我也相信你的CSS display值不正确。 Try this: 试试这个:

 $(document).ready(function () {
        $('.ddlMajor').change(function () {
        if ($(this).val() == 'Other') {
         $('.txtOther').css('display', 'block');
         }
        else {
        $('.txtOther').css('display', 'none');
        }

        });

Or if you do not want to change Markup, use ClientID . 或者,如果您不想更改标记,请使用ClientID Note: This will only work when youve got the javascript contained within the .aspx file 注意:这只有在.aspx文件中包含javascript时才有效

 $(document).ready(function () {
        $('#<%= ddlMajor.ClientID %>').change(function () {
        if ($(this).val() == 'Other') {
         $('#<%= txtOther.ClientID %>').css('display', 'block');
         }
        else {
        $('#<%= txtOther.ClientID %>').css('display', 'none');
        }

        });

Your issue is that when you try to hide the textbox, you set its display to block . 您的问题是,当您尝试隐藏文本框时,将其显示设置为block JQuery uses the display property display: none to hide the textbox. JQuery使用display属性display: none来隐藏文本框。 So what you're doing is overwriting jQuery's hiding. 所以你正在做的是覆盖jQuery的隐藏。 Try this: 试试这个:

$(document).ready(function () {
    $('#ddlMajor').change(function () {
        $('#txtOther').css('display', 'inline');
        if ($(this).val() == 'Other') {
            $('#txtOther').show();
        } else {
            $('#txtOther').hide();
        }
    });
});

You don't need to use classes as references but as the server controls will have a different ID when rendered you can use inline( <%= ddlMajor.ClientID %> ) instead to get the proper ID: 您不需要使用类作为引用,但由于服务器控件在呈现时将具有不同的ID,您可以使用内联(<%= ddlMajor.ClientID%>)来获取正确的ID:

For example: 例如:

<script type="text/javascript">
    $(document).ready(function () {
      $("#<%= ddlMajor.ClientID %>").change(function () {
        if ($(this).val() == 'Other') {
                $('#<%= txtOther.ClientID %>').show();

         }
        else {
                $('#<%= txtOther.ClientID %>').hide();

        }

        });

    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM