在文本框html中插入值选择

I read the discussion on how to check outstretched by HTML select and add in HTML text input.

Does not work in my case having a select dynamic that takes while loop with the data from DB. Could you help me find a solution?

I want to display the value and the text that is chosen in the select, in the form below in a text field.

   <select name="auto" id="auto">
    <option value="false">Auto non presente</option>
     <?php while ($row = mysqli_fetch_array($result_modello)):; ?>
       <option value="<?php echo $row['id_modello']; ?>"><?php echo $row['nome_modello']; ?></option>
     <?php endwhile; ?>
  </select>
  <select name="marca">
    <option value="false">Marca non presente</option>
     <?php while ($row = mysqli_fetch_array($result_marca)):; ?>
       <option value="<?php echo $row['id_marche']; ?>"><?php echo $row['nome_marche']; ?></option>
     <?php endwhile; ?>
  </select>

Try this :

$("input[type=text]").val($("select[name=marca]").val());

Or

 $("input[type=text]").val($("#auto").val());

If you want display both select use this :

var one = $("#auto").val();

var two = $("select[name=marca]").val();

$("input[type=text]").val(one + " , " + two);

you can use .val() for value and .text() for getting text.

var text_val = 'val : ' + $("select").val() + 'text : ' +$("select").text();
$("input[type=text]").val(text_val);

Not using jQuery but with plain old regular Javascript something along these lines should do it.

<form>
    <select id='auto' name='auto'>
        <option value=1>Banana
        <option value=2>Apple
        <option value=3>Cherry
        <option value=4>Pomegranate
        <option value=5>Mango
    </select>
    <div id='results'></div>
</form>

<script type='text/javascript'>
    function display(e){
        var el=e.target;
        var div=document.getElementById('results');
        var value=el.options[el.options.selectedIndex].value;
        var text=el.options[el.options.selectedIndex].text;
        div.innerHTML=value+text;
    }
    document.getElementById('auto').addEventListener('change',display,false);
</script>

this will work

var e = document.getElementById("auto");
var value = e.options[e.selectedIndex].value;
var text = e.options[e.selectedIndex].text;

I thank you for your attention to my post . Only in my case being dynamic text field appear in the select all values ​​. I wished that in the two text boxes in the second form comparissero only two values ​​chosen in the two select .