如何根据选择框下拉菜单自动填充输入表单?

huangapple go评论131阅读模式
英文:

How can i make auto fill an input form based on selectbox dropdown?

问题

我正在尝试根据选择框的选择自动填充输入表单。自动填充文本来自我的数据库。

这是我的代码:

  1. <select style="width: 110px;" class="form-control" name="id_periodik" id="id_periodik" required oninvalid="this.setCustomValidity('Masukkan periodik sparepart!')" oninput="this.setCustomValidity('')">
  2. <?php foreach ($periodik_options as $option) {?>
  3. <option value="<?php echo $option['id_periodik']; ?>">
  4. <?php echo $option['nama_periodik']; ?>
  5. </option>
  6. <?php }?>
  7. </select>
  8. </td>

基于选择自动填充文本:

  1. <td>
  2. <input type="text" class="form-control" id="jumlah_periodik" name="jumlah_periodik[]" readonly>
  3. </td>

脚本:

  1. <script>
  2. $(document).ready(function() {
  3. $('#id_periodik').on('change', function() {
  4. var selectedValue = $(this).val();
  5. $('#jumlah_periodik').val(selectedValue);
  6. });
  7. });
  8. </script>

我已经尝试了这个,但在我的视图中没有任何反应。您可以提供的任何帮助我都非常感激。

英文:

I'm trying to auto fill an input form based on the selection of a selectbox. the auto fill text comes from my database

This is my code:

  1. &lt;select style=&quot;width: 110px;&quot; class=&quot;form-control&quot; name=&quot;id_periodik&quot; id=&quot;id_periodik&quot; required oninvalid=&quot;this.setCustomValidity(&#39;Masukkan periodik sparepart!&#39;)&quot;
  2. oninput=&quot;this.setCustomValidity(&#39;&#39;)&quot;&gt;
  3. &lt;?php foreach ($periodik_options as $option) {?&gt;
  4. &lt;option value=&quot;&lt;?php echo $option[&#39;id_periodik&#39;]; ?&gt;&quot;&gt;
  5. &lt;?php echo $option[&#39;nama_periodik&#39;]; ?&gt;
  6. &lt;/option&gt;
  7. &lt;?php }?&gt;
  8. &lt;/select&gt;
  9. &lt;/td&gt;

text auto fill based on selection:

  1. &lt;td&gt;
  2. &lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;jumlah_periodik&quot; name=&quot;jumlah_periodik[]&quot; readonly&gt;
  3. &lt;/td&gt;

script:

  1. &lt;script&gt;
  2. $(document).ready(function() {
  3. $(&#39;#id_periodik&#39;).on(&#39;change&#39;, function() {
  4. var selectedValue = $(this).val();
  5. $(&#39;#jumlah_periodik&#39;).val(selectedValue);
  6. });
  7. });
  8. &lt;/script&gt;

i've try that but nothing happens in my view. Any help you can give I appreciate it
enter image description here

答案1

得分: -1

  1. 获取选定的 id_periodik
  2. 然后发送 AJAX 请求以从数据库获取数据
  3. 然后将数据设置为 jumlah_periodik
英文:
  1. $(document).ready(function() {
  2. $(&#39;#id_periodik&#39;).on(&#39;change&#39;, function() {
  3. // here is the change
  4. var selectedValue = $(&#39;#id_periodik&#39;).find(&quot;:selected&quot;).val();
  5. $.ajax({
  6. type : &quot;Get&quot;,
  7. url: &#39;your_url&#39;,
  8. data: {id_periodik: selectedValue},
  9. success:function(result){
  10. $(&#39;#jumlah_periodik&#39;).val(result);
  11. }
  12. });
  13. });
  14. });

</script>

  1. Get selected id_periodik
  2. Then send ajax request to get data from database
  3. Then set data to jumlah_periodik
    Hope this work

huangapple
  • 本文由 发表于 2023年7月13日 17:34:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76677922.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定