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

后端 未结 3 1180
野的像风
野的像风 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 14:04

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

    Try this one:

    HTML

    
    
    

    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

    
    

    Jquery

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

    Try in Fiddle

提交回复
热议问题