It is necessary that when you select the string value in select , a text field string below, when the image selection is a file field.

html :

 <label>Choose type</label> <select name="type" id="type"> <option></option> <option value="string">String</option> <option value="image">Image</option> </select> <div id="string" style="display:none;"> <input type="text"> </div> <div id="image" style="display:none;"> <input type="file"> </div> 

js

 $('#type').on('change',function(){ var selection = $(this).val(); switch(selection){ case "string": $("#string").show() break; case "image": $("#image").show() break; default: $("#string").hide() } }); 

So that the bottom field does not display when nothing is selected, I write the function hide() . But there it turns out to score only one value. That is, the string is removed, but the image remains.

Question: how to add 2 values ​​to hide() , or redo the code in js ? In js it is not strong, I found an example, I need it for the yii framework.

enter image description here

That's when string is selected.

enter image description here

But when I select image, then string sells well, either. Confusedly wrote, but I hope it is clear enter image description here

  • and again php label in js question, why? - Naumov
  • @Naumov well there sounded that нужно для yii фреймворка =) And so - no one bothers to remove the extra label - Alexey Shimansky

2 answers 2

this is a matter of logic, you show the elements, but you do not hide

 switch(selection){ case "string": $("#string").show() $("#image").hide() break; case "image": $("#image").show() $("#string").hide() break; 

and put commas at the end of the line

     $('#type').on('change',function(){ var selection = $(this).val(); $("#string,#image").hide(); selection ? $("#"+selection).show() : false; });