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

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

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

问题

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

这是我的代码:

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

基于选择自动填充文本:

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

脚本:

<script>
  $(document).ready(function() {
    $('#id_periodik').on('change', function() {
      var selectedValue = $(this).val();
      $('#jumlah_periodik').val(selectedValue);
    });
  });
</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:

&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;
oninput=&quot;this.setCustomValidity(&#39;&#39;)&quot;&gt;
&lt;?php foreach ($periodik_options as $option) {?&gt;
&lt;option value=&quot;&lt;?php echo $option[&#39;id_periodik&#39;]; ?&gt;&quot;&gt;
&lt;?php echo $option[&#39;nama_periodik&#39;]; ?&gt;
&lt;/option&gt;
&lt;?php }?&gt;
&lt;/select&gt;
&lt;/td&gt;

text auto fill based on selection:

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

script:

&lt;script&gt;
$(document).ready(function() {
$(&#39;#id_periodik&#39;).on(&#39;change&#39;, function() {
var selectedValue = $(this).val();
$(&#39;#jumlah_periodik&#39;).val(selectedValue);
});
});
&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
英文:
$(document).ready(function() {
    $(&#39;#id_periodik&#39;).on(&#39;change&#39;, function() {
        // here is the change
        var selectedValue = $(&#39;#id_periodik&#39;).find(&quot;:selected&quot;).val();

        $.ajax({
                type : &quot;Get&quot;,
                url: &#39;your_url&#39;,
                data: {id_periodik: selectedValue},
                success:function(result){
                    $(&#39;#jumlah_periodik&#39;).val(result);
                }
            });
    });
});

</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:

确定