简体   繁体   中英

to make value selected obtained from javascript

my code is as follows and m getting the value from javascript

<script type="text/javascript">
function show(desig){
document.getElementById("designation").value=desig;
}
</script>



<select id="designation" name="designation">    
 <?php
        while($row_address=mysql_fetch_array($loc))
                         {
          <option value="" selected="selected"><?=$row_address['location']?></option>
          <?php }?>
        </select>

but m not able to find the solution to tick that particular value obtained from javascript in dropdown

I tested your code tested a simplified version of your code and it works fine. The one thing to note is that you must give it a value that does exist as an option.

Here is the code i used in full:

<!DOCTYPE html>
<html>
  <body>
    <select id="designation" name="designation">    
      <option value="some value" selected="selected">some text</option>
      <option value="some value 2" selected="selected">some other text</option>
    </select>
  </body>
</html>

<script type="text/javascript">
  function show(desig){
    document.getElementById("designation").value=desig;
  }

  show('some value 2');
</script>

Note that if I called show('blah') it would not have worked since that isn't a valid option value.

Edit: your php code seems to have a few syntax issues and you also do not specify a value= property other than an empty string - which is probably a big part of why it isn't working. Try

<?php
    while($row_address=mysql_fetch_array($loc)) {
        $location = $row_address['location'];
        echo "<option value='$location'>$location</option>";
    }
?>

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