文本框更改选择

时间:2013-03-15 01:40:09

标签: php javascript mysql combobox textbox

我有以下代码:

$sql="SELECT * from customer_billing where sequence = '".$_GET["seq"]."' ";
    $rs=mysql_query($sql,$conn) or die(mysql_error());
    $result=mysql_fetch_array($rs);
    ?>
    <script type="text/javascript">
    function producttype(data) {
    document.getElementById ("producttype").value = data.value;
    }
    function sagenominalcode(data) {
    document.getElementById ("sagenominalcode").value = data.value;
    }
    </script>
    <form method="post" action="editbillingline.php">
    <table width="800" border="0" cellspacing="5" cellpadding="5">
      <tr>
        <td width="50%"><strong>Product Type: </strong>
          <br />
        <select name="producttype-dropdown" id="producttype-dropdown" onchange="producttype(this)">
        <option value="">none</option>
        <?php
        $sql2="SELECT * from customer_billing group by producttype order by producttype ";
        $rs2=mysql_query($sql2,$conn) or die(mysql_error());
        $display='';
        while($result2=mysql_fetch_array($rs2))
        {
            $display.='<option value="'.$result2["producttype"].'"';
            if($result["producttype"]==$result2["producttype"]){$display.=' selected="selected" ';}
            $display.='>'.$result2["producttype"].'</option>';
        }
        echo($display);
        $display='';
        ?>
        </select>
        <input name="producttype" id="producttype" type="text" size="30" value="<?php echo $result["producttype"]; ?>" /></td>

所以它从一个工作正常的数据库中选择,然后当你在下拉框中更改一个选项时,它需要将值放入它下面的文本框中。如您所见,组合框是从mysql数据库生成的。

关于如何让它将所选项目的值插入文本框的任何想法?

编辑:

<script type="text/javascript">
function producttype(data) {
document.getElementById ("producttype").value = data.value;
}
function sagenominalcode(data) {
document.getElementById ("sagenominalcode").value = data.value;
}
</script>
<form method="post" action="editbillingline.php">
<table width="800" border="0" cellspacing="5" cellpadding="5">
  <tr>
    <td width="50%"><strong>Product Type: </strong>
      <br />
    <select name="producttype-dropdown" id="producttype-dropdown" onchange="producttype(this)">
    <option value="">none</option>
    <option value="PC Maintenance">PC Maintenance</option><option value="VoIP Telephony" selected="selected" >VoIP Telephony</option>        </select>
    <input name="producttype" id="producttype" type="text" size="30" value="VoIP Telephony" /></td>

1 个答案:

答案 0 :(得分:2)

试试这个

<script>
window.onload = function(){
    document.getElementById('producttype-dropdown').addEventListener('change', function(){
        document.getElementById('producttype').value = this.options[this.selectedIndex].value;
    });
}
</script>