如果我选择该选项,如何将代码也打印出代码名

bxgwgixi  于 2021-09-23  发布在  Java
关注(0)|答案(2)|浏览(316)
<tr>
        <td>Credit Account No: </td>
        <td><select name="debitacc">
        <?php
            $sql = "SELECT * FROM account";
            $res = mysqli_query($conn, $sql);
            $count = mysqli_num_rows($res);

            if($count>0)
            {
                while($row=mysqli_fetch_assoc($res))
                {
                    $code = $row['code'];
                    $id = $row['id'];

                    ?>
                    <option value="<?php echo $id;?>"><?php echo $code; ?></option>
                    <?php
                }
            }
            else
            {
                echo "<option value='0'>No Account.</option>";
            }
        ?>

        </select>
        </td>

        <td>Account Name: </td>
        <td><input type="text" name="creditaccname" placeholder="Enter Your Account Name"></td>
        </tr>

我已在phpmyadmin中保存了我的帐户

id  code name
1   1234 bank charge
2   9988 CIMB

如何选择代码 1234 会自动回音吗 <td> Acc 代码名称

31moq8wy

31moq8wy1#

很容易将代码名值存储为选项中的数据属性,然后在选择时使用 change 事件

function showAccName(v) {
  let name = document.querySelector('select[name=debitacc] option:checked').dataset.name;
  document.querySelector('#accName').innerHTML = name ? `Code Type: <strong>${name}</strong>` : '';
}
<table><tr>
  <td>Credit Account No: </td>
  <td>
    <select name="debitacc" onchange='showAccName(this.value)'>
      <option value='0'>No Account.</option>
      <option value='1' data-name="bank charge">1234</option>
      <option value='2' data-name="CIMB">9988</option>
    </select>
  </td>
  <td>Account Name: </td>
  <td><input type="text" name="creditaccname" placeholder="Enter Your Account Name"></td>
</tr></table>

    <div id='accName'></div>
np8igboo

np8igboo2#

代码概念示例:生成html/jabascript,如下所示

<select name="DebitAccount" onchange="document.getElementById('AccountName').innerHTML = this.options[this.selectedIndex].getAttribute('AccountName');">
    <option value="AccountID" AccountName="AccountName">AccountCode</option>
    ...
</select>

Account name: <span id="AccountName"></span>

免责声明:关于内联javascript的使用和正确的html定义,存在javascript确认问题。你应该相应地遵循它们,我只是想在这个概念上提供帮助。

相关问题