简体   繁体   English

如何根据所选选项在数据库 MySQL 的文本框中显示数据?

[英]How to display data in textbox from database MySQL based on selected option?

I have a table "tb_seri" in MySQL, with columns "id_seri", "nm_seri" and "value_seri"我在 MySQL 中有一个表“tb_seri”,其中包含“id_seri”、“nm_seri”和“value_seri”列

So, I want to select "nm_seri" in the select option, and then "value_seri" will appear in the textbox afterwards based on "nm_seri" selected所以,我想在 select 选项中的 select "nm_seri",然后根据 "nm_seri" 选择 "value_seri" 将出现在文本框中

Here is the code to select "nm_seri" from the database这是数据库中 select "nm_seri" 的代码

<select name="nm_seri" id="nm_seri" onchange="myfunction()" required>
    <option disabled selected>-Pilih Seri-</option>
        <?php
             $qu="Select DISTINCT nm_seri from tb_seri";
             $res=$koneksi->query($qu);

             while($r=mysqli_fetch_row($res))
             { 
                 echo "<option data-value='$r[1]' value='$r[0]'> $r[0] </option>";
             }
        ?> 
</select>

And I've tried various codes from here, but I'm still confused.我已经从这里尝试了各种代码,但我仍然感到困惑。

And this textbox code to display "value_seri"这个文本框代码显示“value_seri”

<input type="text" name="value_seri" id="value_seri" value="" disabled><br></td>
<script>
    function myFunction()
    {
        var value_seri = $('#value').find(':selected').data('nm_seri');
                            $('#value').val(value_seri);
    }
</script>

In your current js code there is no value it should be nm_seri .Then, you are getting data attribute using data('nm_seri') it should be data('value') and show this value inside input box using $('#value_seri').val(value_seri);在您当前的 js 代码中,没有value应该是nm_seri 。然后,您使用data('nm_seri')获取数据属性,它应该是data('value')并使用$('#value_seri').val(value_seri);在输入框中显示此值$('#value_seri').val(value_seri); .Also,your function name should match with onchange="myfunction()" so change that as well. .此外,您的 function 名称应与onchange="myfunction()"匹配,因此也要更改。

Demo Code :演示代码

 function myfunction() { var value_seri = $('#nm_seri').find(':selected').data('value'); $('#value_seri').val(value_seri); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select name="nm_seri" id="nm_seri" onchange="myfunction()" required> <option disabled selected>-Pilih Seri-</option> <option data-value='1' value='soemthing'> soemthing</option> <option data-value='2' value='soemthing2'> soemthing2</option> <option data-value='3' value='soemthin3g'> soemthing3</option> </select> <input type="text" name="value_seri" id="value_seri" value="" disabled><br>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaScript基于选定选项显示asp:TextBox - JavaScript to display asp:TextBox based on selected option 如何根据 php 中 combobox 中的选定值在数据库中的文本框中显示该值? - How can I display value in textbox from database that value based on selected value from combobox in php? 如何在一个 select 选项中获取选定值,并在此基础上,从 MySQL 表中获取数据,以相同形式显示另一个 select 选项 - How to get selected value in one select option and based on that, fetch data from MySQL table to show another select option in the same form 根据选择的选项显示 - Display based on Option Selected 如何根据所选选项显示两个链接? - How to display two links based on the selected option? 如何在数据库MYSQL的下拉列表中选择选项显示输入中的数据? - How to display data in input from selecting option in drop down with database MYSQL? 选择的选项从PHP中的数据库中获取数据 - selected option fetch data from database in php 根据来自form_for select的所选值显示空文本框 - Display empty textbox based on selected value from form_for select 如何在选择时从数据库中获取多个值并在文本框中显示<option value="in jsp">在 jsp</option> - How to fetch multiple values and display in textbox from database on selection of <option> in jsp 从数据库中检索数据并显示在文本框中 - Retrieve data from database and display in textbox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM