在php下拉列表中使用值和标签

时间:2015-06-01 10:50:27

标签: php drop-down-menu onchange

我有这个代码从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" />

由于

2 个答案:

答案 0 :(得分:1)

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

假设下拉列表的ID为“drop”,文本字段的id为“txt_id”。现在您可以使用以下代码:

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

答案 1 :(得分:0)

您可以使用以下代码: 您还可以在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>