简体   繁体   English

使从javascript中获得选择的价值

[英]to make value selected obtained from javascript

my code is as follows and m getting the value from javascript 我的代码如下,并从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 但是我无法找到解决方案以勾选从下拉列表中的javascript中获得的特定值

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. 请注意,如果我调用show('blah') ,它将无效,因为这不是有效的选项值。

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. 编辑:您的php代码似乎有一些语法问题,并且您也未指定value=属性(空字符串除外)-这可能是为什么它不起作用的很大一部分。 Try 尝试

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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