【问题标题】:use value and label in php dropdown在 php 下拉列表中使用值和标签
【发布时间】:2015-06-01 10:50:26
【问题描述】:

我有这段代码可以从 MySQL 数据库中提取值和标签并填充一个下拉框,在更改它时将值放在文本字段中,但我想要标签而不是值。

任何指针都会很好..

<select name="CompanyInternalID" autofocus class="textBox" id="CompanyInternalID" style="width:300px" onchange="document.form1.CompName.value=this.value">
                      <?php
do {  
?>
                      <option value="<?php echo $row_rsCustomerList['AKA']?>"><?php echo $row_rsCustomerList['CustomerName']?></option>
                      <?php
} while ($row_rsCustomerList = mysql_fetch_assoc($rsCustomerList));
  $rows = mysql_num_rows($rsCustomerList);
  if($rows > 0) {
      mysql_data_seek($rsCustomerList, 0);
      $row_rsCustomerList = mysql_fetch_assoc($rsCustomerList);
  }
?>
                </select>
                  <input type="text" name="CompName" class="textBox" style="width:180px" id="CompName" />

谢谢

【问题讨论】:

    标签: php drop-down-menu onchange


    【解决方案1】:

    您可以使用 jQuery 获取选定的值并放入所需的文本字段中。

    假设下拉列表的 id 是 "drop" 并且文本字段的 id 是 "txt_id" 。现在您可以使用以下代码:

    $("#drop").change(function () { 
         $("#txt_id").val($(this).val());
     });
    

    【讨论】:

    • 谢谢,没用过 jQuery,你能给我提示一下它的去向吗?
    【解决方案2】:

    您可以使用以下代码: 你也可以在http://www.w3schools.com/jquery/学习jQuery

        <!doctype html>
    
       <html>
    
    <head>
    
    <title>My Page</title>
    
    <meta name="viewport" content="width=device-width, initial-scale=1">
    
    <script src="https://code.jquery.com/jquery-1.8.2.min.js"></script>
    
    </head>
    
    <body>
    <select name="CompanyInternalID" autofocus class="textBox" id="CompanyInternalID" style="width:300px" > <?php do { ?> <option value="<?php echo $row_rsCustomerList['AKA']?>"><?php echo $row_rsCustomerList['CustomerName']?></option> <?php } while ($row_rsCustomerList = mysql_fetch_assoc($rsCustomerList)); $rows = mysql_num_rows($rsCustomerList); if($rows > 0) { mysql_data_seek($rsCustomerList, 0); $row_rsCustomerList = mysql_fetch_assoc($rsCustomerList); } ?> </select> <input type="text" name="CompName" class="textBox" style="width:180px" id="CompName" />
    
    <script>
    $("#CompanyInternalID").change(function () { $("#CompName").val($(this).val());
     }
    );
    </script>
    </body>
    
    </html>
    

    【讨论】:

      猜你喜欢
      • 2016-01-28
      • 2011-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-06
      • 2017-03-01
      相关资源
      最近更新 更多