jQuery - How to show/hide text box based on selected drop down

后端 未结 6 1749
广开言路
广开言路 2021-01-01 01:53

Sorry if this is extremely obvious, but I have looked and looked for a solution but haven\'t found anything. I\'m very new to jQuery, so even looking for what I want to do h

相关标签:
6条回答
  • 2021-01-01 02:28

    I've had to do this very thing. Here's the code I used:

    $(function() {
        var 
        jqDdl = $('#ddl'),
        onChange = function(event) {
            if ($(this).val() === 'Other') {
                $('#otherTxtbox').show();
                $('#otherTxtbox').focus().select();
            } else {
                $('#otherTxtbox').hide();
            }
        };
        onChange.apply(jqDdl.get(0)); // To show/hide the Other textbox initially
        jqDdl.change(onChange);
    });
    
    0 讨论(0)
  • 2021-01-01 02:30
    $("option").bind('click', function(){
        var selected = $(this).val();
        alert(selected+' selected');
        if (selected == '1') { /* do anything you want */ }
        if (selected == '2') { /* do anything you want */ }
        //etc ...
    });
    
    0 讨论(0)
  • 2021-01-01 02:31

    If you have a select box, i.e.

    <select class="myselect">
       ....
    </select>
    

    you can bind to .change(), i.e.

    $('.myselect').change(function() {
           --> do show hide here.
    });
    

    Look up jquery .show() and .hide(). (http://api.jquery.com/show and http://api.jquery.com/hide).

    0 讨论(0)
  • 2021-01-01 02:35
    $(function() {
        if($("#yourDropDown").val() == 0) //I'm supposing the "Other" option value is 0.
             $("#yourTextBox").hide();
    });
    
    0 讨论(0)
  • 2021-01-01 02:35

    Please try this code, hope it may work

        <asp:TextBox ID="txtOtherEntity" runat="server" style="display:none;" ></asp:TextBox>
    
        $(function() {
    $("#ddlEntity").change(function() {
        var selectedVal = $('option:selected', this).text();
        if(selectedVal == "Other")
        {
             $('#txtOtherEntity').css('display', 'block');
    
        }
        else
        {
            $('#txtOtherEntity').css('display', 'none');
        }
    });
    
    0 讨论(0)
  • 2021-01-01 02:40

    Assuming by default your html has both boxes and the "Other" box is hidden.

    val = $('#myselect').val();
    
    switch( val ) {
    
      case "Other":
    
        $('#mybox').hide();
    
        $('#myotherbox').show();
    
        break;
    
    }
    
    0 讨论(0)
提交回复
热议问题