Drop-down list

I think this is a simple answer, but not sure about the best method, and I'm new to forms.

I want to have a dropdown list with model numbers. When a specific model number is selected, it displays a form with the corresponding names.

eg.

Model 1 - when selected - displays input field 1 and input field 2

Model 2 - when selected - displays input field 1 and input field 2 and input field 3

Model 3 - when selected - displays input field 1 and input field 4 and input field 5

I would like this to happen dynamically.

Help me greatly appreciate the weather when you write code or connect me with a tutorial or example site

Thank you

+3
source share
3 answers

HTML

<select id="myselect">
  <option value="Model 1">Model 1</option>
  <option value="Model 2">Model 2</option>
</select>

<div id="Form1" class="forms">Form 1 Contents</div>
<div id="Form2" class="forms">Form 2 Contents</div>

jQuery

$(function() {
    $(".forms").hide();
    $("#myselect").change(function() {
        switch($(this).val()){ 
            case "Model 1":
                $(".forms").hide().parent().find("#Form1").show();
                break;
            case "Model 2":
                $(".forms").hide().parent().find("#Form2").show();
                break;
        }
    });
});

. jsFiddle: http://jsfiddle.net/6PtuN/

+1
<select onchange="JavaScript: showAppropriateForm( this.value );">
  <option value="Model 1">Model 1</option>
  <option value="Model 2">Model 2</option>
</select>

function showAppropriateForm( v )
{
  document.getElementById( "Form1" ).style.visibility = "hidden";
  document.getElementById( "Form2" ).style.visibility = "hidden";

  if( v == "Model 1" )
  {
    document.getElementById( "Form1" ).style.visibility = "visible";
  }
  else if( v == "Model 2" )
  {
    document.getElementById( "Form2" ).style.visibility = "visible";
  }
}
+1
$('.button').click(function(event){
    switch (event.target) {
    case $('#option1'):
         //load input field 1 and 2
    break;
    case $('#option2'):
         //load input field 1, 2, and 3
    break;
});

Not sure of the syntax for finding an identifier using a switch, but you could just as easily use the if / else if / else if statement.

0
source

Source: https://habr.com/ru/post/1755325/


All Articles