Add input-box when selecting a specific option into select drop down

后端 未结 3 1181
野的像风
野的像风 2021-01-14 13:02

i need to add input to a select option when it is selected. whenever the user selects \'other\' an input box is there for the user to enter in data.

html:

         


        
相关标签:
3条回答
  • 2021-01-14 13:42

    See it in action here.

    HTML:

    <select id="choose">
        <option>Choose Your Name</option>
        <option>Frank</option>
        <option>George</option>
        <option value="other">Other</option>
    </select>
    <label id="otherName">Enter your Name
        <input type="text" name="othername" />
    </label>
    

    jQuery:

    $(document).ready(function() {
        $("#choose").on("change", function() {
            if ($(this).val() === "other") {
                $("#otherName").show();
            }
            else {
                $("#otherName").hide();
            }
        });
    });
    

    Note the value="other" attribute on the "Other" option. That's how the script determines if the "Other" option is selected.

    Hope this helps!

    0 讨论(0)
  • 2021-01-14 13:52

    Here is a pure javascript version, no jQuery needed:

    <script>
    // Put this script in header or above select element
        function check(elem) {
            // use one of possible conditions
            // if (elem.value == 'Other')
            if (elem.selectedIndex == 3) {
                document.getElementById("other-div").style.display = 'block';
            } else {
                document.getElementById("other-div").style.display = 'none';
            }
        }
    </script>
    
    <select id="mySelect" onChange="check(this);">
            <option>Choose Your Name</option>
            <option>Frank</option>
            <option>George</option>
            <option>Other</option>
    </select>
    <div id="other-div" style="display:none;">
            <label>Enter your Name
            <input id="other-input"></input>
            </label>
    </div>
    

    jsFidle

    As stated before, add onChange event, link it to function and there process what should be displayed etc.

    0 讨论(0)
  • 2021-01-14 14:04

    You can use jquery .change() to bind change event of an element.

    Try this one:

    HTML

    <select>
      <option>Choose Your Name</option>
      <option>Frank</option>
      <option>George</option>
      <option>Other</option>
    </select>
    <label style="display:none;">Enter your Name
    <input></input>
    </label>
    

    Jquery

    $('select').change(function(){
         if($('select option:selected').text() == "Other"){
            $('label').show();
         }
         else{
            $('label').hide();
         }
     });
    

    Try in Fiddle

    Updated:

    You can also add an input-box dynamically -

    HTML

    <select>
      <option>Choose Your Name</option>
      <option>Frank</option>
      <option>George</option>
      <option>Other</option>
    </select>
    

    Jquery

    $('select').change(function(){
       if($('select option:selected').text() == "Other"){
            $('html select').after("<label>Enter your Name<input></input></label>");
       }
       else{
            $('label').remove();
       }
    });
    

    Try in Fiddle

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