简体   繁体   中英

Get selected option from drop down list in textbox

I have defined dropdown list with first option 'lisence' which is selected by default and the rest of options are selected from 'id' column of mysql database. I want when I select the option other than the first option, to get assigned to textbox value.

Search For:<input type="text" name="valuetosearch" value="">
<select name="Option">
<?php 
$selectOption = $_GET['Option'];
?>
<option>Lisence</option>
<?php
$res = $conn->query($sql);
while($r=$res->fetch_assoc())
{
?>
<option value="<?php echo $r["id"];   ?>">
<?php echo $r["id"];   ?>
</option>
<?php
}
?>

Try below code. Change event has been added to the select dropdown. When you change the dropdown its value will put into the textbox valuetosearch .

<script>
    $(document).ready(function(){
        $('[name="Option"]').on('change',function(e){ alert($(this).val());
            var val = $(this).val() === 'Lisence' ? '' : $(this).val(); 
            $('[name="valuetosearch"]').val(val);
        })
    });


    </script>

You can use jquery for achieving that ,first give class="abc" to your select box.Now,use below code :

Search For:<input type="text" name="valuetosearch" value="" class="ab">

Jquery :

  <script>
    $(document).on("change",".abc",function(){
        var value=$('.abc').val();//getting value of select box
        console.log(value);
       if(value != 'Lisence'){
       $(".ab").val(value);//puting value in textbox
       }
    }); 
  </script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM