I have a table “tb_seri” in MySQL, with columns “id_seri”, “nm_seri” and “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
Here is the code to select “nm_seri” from the database
<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”
<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>
Advertisement
Answer
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);
.Also ,your function name should match with onchange="myfunction()"
so change that as well.
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>