doucitao2944 2017-11-05 09:29
浏览 75
已采纳

根据下拉选择自动显示数据

Basically, I have a table with two column: 'Kode Barang' (Item ID) and 'Nama Barang' (Name of Item). The first column is a dropdown option which it's data get populated dynamically from another table. If a user select an Item ID, then the second column will automatically show the name of the item.

Let's say that I've only two row as this code below:

<HTML>
<table id="theTable" border="1">

<thead>
    <tr>
        <th> Kode Barang </th>
        <th> Nama Barang </th>
    <tr> 
</thead>

<tbody>
    <tr>
        <td type="text" name="kode_barang" id="kode_barang"/readonly>
            <?php  
                mysql_connect("localhost","root","");  
                mysql_select_db("skripsi_1");  
                $result = mysql_query("select * from input_data_barang");  
                $jsArray = "var kode_barang = new Array();
";  
                echo '<select name="kode_barang" onchange="changeValue(this.value)">';  
                echo '<option></option>';  
                while ($row = mysql_fetch_array($result)) {  
                echo '<option value="' . $row['kode_barang'] . '">' . $row['kode_barang'] . '</option>';  
                $jsArray .= "kode_barang['" . $row['kode_barang'] . "'] = {name:'" . addslashes($row['nama_barang']) . "',desc:'".addslashes($row['nama_barang'])."'};
";  
                } 
                echo '</select>';  
            ?>  
        </td>

        <td><input type="text" name="nama_barang" id="nama_barang"/readonly>
            <script type="text/javascript">
                <?php echo $jsArray; ?>
                function changeValue(id){
                document.getElementById('kode_barang').value = kode_barang[id].name;
                document.getElementById('nama_barang').value = kode_barang[id].desc;
                };
            </script>
        </td>
    </tr>

    <tr>
        <td type="text" name="kode_barang" id="kode_barang"/readonly>
                            <?php  
                mysql_connect("localhost","root","");  
                mysql_select_db("skripsi_1");  
                $result = mysql_query("select * from input_data_barang");  
                $jsArray = "var kode_barang = new Array();
";  
                echo '<select name="kode_barang" onchange="changeValue(this.value)">';  
                echo '<option></option>';  
                while ($row = mysql_fetch_array($result)) {  
                    echo '<option value="' . $row['kode_barang'] . '">' . $row['kode_barang'] . '</option>';  
                    $jsArray .= "kode_barang['" . $row['kode_barang'] . "'] = {name:'" . addslashes($row['nama_barang']) . "',desc:'".addslashes($row['nama_barang'])."'};
";  
                } 
                echo '</select>';  
                ?>  
        </td>

        <td><input type="text" name="nama_barang" id="nama_barang"/readonly>
            <script type="text/javascript">
            <?php echo $jsArray; ?>
            function changeValue(id){
            document.getElementById('kode_barang').value = kode_barang[id].name;
            document.getElementById('nama_barang').value = kode_barang[id].desc;
            };
            </script>
        </td>
    </tr>
</table>
</HTML> 

The first row works perfectly. The problem is in the second row. If I select an option from the dropdown, then name of the item doesn't appear in the second row, but appear in the first row instead. Would anybody please show me how to fix this? Thank you.

  • 写回答

1条回答 默认 最新

  • dt3358 2017-11-05 09:33
    关注

    You are appending your values using:

    document.getElementById('kode_barang').value = kode_barang[id].name;
    document.getElementById('nama_barang').value = kode_barang[id].desc;
    

    The problem is, that there is an Element with the ID kode_barang/nama_barang in BOTH rows. So you have 2 Elements for the ID's. Javascript appereantly just decides only to take the first one. Just rename them in the second row to "kode_barang2" and "nama_barang2" and when setting the values change the names too:

    document.getElementById('kode_barang2').value = kode_barang[id].name;
    document.getElementById('nama_barang2').value = kode_barang[id].desc;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题