display/hide textbox based on drop down list

前端 未结 8 1622
灰色年华
灰色年华 2021-01-05 23:46

For-example I\'ve got a code:

提交评论

  • 2021-01-05 23:59

    JQuery

    $(document).ready(function() {
            $("select").change(function() {
                $(this).find("option:selected").each(function() {
                    if ($(this).attr("value") == "class") {
                        $(".box").not(".class_name").hide();
                        $(".class_name").show();
                        $(".box").not(".class_name").val("");
                    } 
    
                    else if ($(this).attr("value") == "ref") {
                        $(".box").not(".ref_id").hide();
                        $(".ref_id").show();
                        $(".box").not(".ref_id").val("");
                    }
    
                    else {
                        $(".box").hide();
                    }
                });
            }).change();
        });
    

    HTML

    <div>
                <select>
                    <option>Choose</option>
                    <option value="class">Class</option>
                    <option value="ref">Particular</option>
                </select>
            </div>
            <div class="class_name box">
                Class Name: <input type="text" name="c">
            </div>
            <div class="ref_id box">
                Reference_id : <input type="text" name="r">
            </div>
    
    0 讨论(0)
  • 2021-01-06 00:03
    <form name="myform">
    <table>
    <td>
    <select name="one" id="mySelect">
    <option value="" selected="selected">Select...</option>
    <option value="1">1</option>
    <option value="2">3</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="other">Other</option>
    </select>
    <input type="textbox" name="other" style="visibility:hidden;"/>
    </td>
    </table>
    </form>
    
    <script type="text/javascript">
    $('#mySelect').bind('onchange',function(){
    if (this.value==='other')
    {
    this.myform['other'].style.visibility='visible'
    }
    else {
    this.myform['other'].style.visibility='hidden'};
    }
    </script>
    
    0 讨论(0)
  • 2021-01-06 00:06
        <form name="myform">
           <table>
             <td>
               <select name="one" onchange="if (this.options[this.selectedIndex].value =='other'){document.myform['other'].style.visibility='visible'}else {document.myform['other'].style.visibility='hidden'};">
                 <option value="" selected="selected">Select...</option>
                 <option value="1">1</option>
                 <option value="2">3</option>
                 <option value="3">3</option>
                 <option value="4">4</option>
                 <option value="5">5</option>
                 <option value="6">6</option>
                 <option value="7">7</option>
                 <option value="other">Other</option>
              </select>
             <input type="textbox" name="other" style="visibility:hidden;"/>
           </td>
        </table>
     </form>​
    
    0 讨论(0)
  • 2021-01-06 00:06

    this.selectedIndex returns an integer not a string. Something like 0,1,2 and probably 8 for 'others'. 8 == 'other' can never be true

    try this

    onchange="if (this.options[this.selectedIndex].value =='other')"
    
    0 讨论(0)
  • 2021-01-06 00:07
    $(document).ready(function () {
            $('#types').change(function () {
                if ($('#types').val() == 'Other') {
                    $('#other').show();
                }
                else {
                    $('#other').hide();
                }
            });
        });
    
    
    
    <body>
    <form id="form1" runat="server">
        <div>
    
       <select id="types" name="types">
                <option value="Type 1">Type 1</option>
                <option value="Type 2">Type 2</option>
                <option value="Type 3">Type 3</option>
                <option value="Other">Other</option>
            </select>
    
            <input type="text" id="other" name="other" style="display: none;" />
        </div>
    </form>
    

    0 讨论(0)
  • 提交回复
    热议问题