简体   繁体   English

动态ajax表单选择框自动填充

[英]dynamic ajax form select box autofill

I'm strugglin to make a PHP Form with Dynamic Fields with Auto Fill other fields in JQuery, so far I can add new fields and the autofill works just fine but only for the first field.我正在努力制作一个带有动态字段的 PHP 表单,并在 JQuery 中自动填充其他字段,到目前为止我可以添加新字段并且自动填充工作得很好,但仅适用于第一个字段。

在此处输入图片说明

The autofill drop down only appears on the first input.自动填充下拉菜单仅出现在第一个输入中。 How can I make all dynamic form input work with the autofill?如何使所有动态表单输入与自动填充一起使用? For example I have 2 fields.例如我有 2 个字段。 Items_name and Total_stock in dynamic form.动态形式的 Items_name 和 Total_stock。 I want to if I select Items_name.如果我选择 Items_name,我想。 Autofill for field total_stock.自动填充字段 total_stock。

Here is my ajax code :这是我的 ajax 代码:

<script language="javascript">
    function AddMasterDetail() {
            var idf = document.getElementById("idf").value;
            stre="<div class='form-group'  id='srow" + idf + "'><div class='controls'>";

            stre=stre+" <div class='col-xs-2'>";
            stre=stre+"<select placeholder='Items Name' class='form-control input-sm' name='items_id[]' id='items_id' onchange='return autofill();'>"
                        +"<option value='' disabled selected>Please Select</option>"
                        +"<?php foreach($v_items_stock as $row){ echo "<option value='$row->items_id'>$row->items_name</option>"; } ?></select>";
            stre=stre+"</div>";

            stre=stre+"<div class='col-xs-2'>";
            stre=stre+"<input class='form-control input-sm' id='total_stock' placeholder='Total Stock'  name='total_stock[]' />";
            stre=stre+"</div>";         


            stre=stre+"<div class='col-xs-1'> <button type='button' class='btn btn-danger btn-sm' title='Hapus Rincian !' onclick='removeFormField(\"#srow" + idf + "\"); return false;'><i class='glyphicon glyphicon-remove'></i></button></div>";

            $("#divItems").append(stre);    
            idf = (idf-1) + 2;
            document.getElementById("idf").value = idf;
        }
    function removeFormField(idf) {
            $(idf).remove();
        }

    function autofill(){
        var items_id = document.getElementById('items_id').value;
        $.ajax({
               url:"<?php echo base_url();?>transaction_sending/cari",
               data:'&items_id='+items_id,
               success:function(data){
               var hasil = JSON.parse(data);  

        $.each(hasil, function(key,val){ 

        document.getElementById('items_id').value=val.items_id;
               document.getElementById('total_stock').value=val.total_stock;


            });
        }
               });
    }   

</script>

The problem is that you're re-using "items_id" as the ID for lots of elements.问题是您正在重新使用“items_id”作为许多元素的 ID。 But IDs must be unique.但 ID 必须是唯一的。 When you run document.getElementById('items_id') (which can only return one element) the code has no way to know which of the multiple elements with that ID you're actually referring to.当您运行document.getElementById('items_id') (它只能返回一个元素)时,代码无法知道您实际指的是具有该 ID 的多个元素中的哪一个。

Now, as I understand it you want to have a relationship between the select and the input box next to it.现在,据我所知,您希望在选择和旁边的输入框之间建立关系。 The value of the select must affect the value of the input box, it seems. select的值肯定会影响输入框的值,看起来。 Therefore you a way need to identify the correct input box which relates to the select whose value has been changed.因此,您需要确定与值已更改的选择相关的正确输入框。

There are many ways you could do this, but one simple way way is to set a data-attribute on the <select containing the unique identifier of the row (you can use idf for this).有很多方法可以做到这一点,但一种简单的方法是在<select上设置一个包含行的唯一标识符的数据属性(您可以为此使用idf )。 You then set the same value as the id of the related input box.然后设置与相关输入框的id相同的值。 Then when the "change" event happens, you can get the data-attribute from the select which triggered the event, and use it to select the correct input ID to update.然后,当“更改”事件发生时,您可以从触发事件的选择中获取数据属性,并使用它来选择正确的输入 ID 进行更新。

I've also used more jQuery syntax for this, since you get unobtrusive event handling, and also the syntax is a bit briefer - it works nicely for this scenario.我还为此使用了更多的 jQuery 语法,因为您可以获得不显眼的事件处理,而且语法也更简洁一些 - 它非常适合这种情况。

First, change第一,改变

stre=stre+"<select placeholder='Items Name' class='form-control input-sm' name='items_id[]' id='items_id' onchange='return autofill();'>"

to

stre=stre+"<select placeholder='Items Name' class='form-control input-sm autofill' name='items_id[]' id='items_id_" + idf + "' data-idf='" + idf + "' >"

Next, change接下来换

stre=stre+"<input class='form-control input-sm' id='total_stock' placeholder='Total Stock'  name='total_stock[]' />";

to

stre=stre+"<input class='form-control input-sm' id='total_stock_" + idf + "' placeholder='Total Stock'  name='total_stock[]' />";

And then replace the whole "autofill()" function with this event handler:然后用这个事件处理程序替换整个“autofill()”函数:

$(document).on("change", ".autofill", function(e) {
  var select = $(this);
  var item_id = select.val();
  var idf = select.data("idf");

  $.ajax({
    url:"<?php echo base_url();?>transaction_sending/cari",
    method: "GET",
    data: { items_id: item_id },
    dataType: "json"
  }).done(function(hasil) {
    $("#total_stock_" + idf).val(hasil[0].total_stock);
  });
});

I have made the assumption here (pending a comment from you) that we only ever want to use the stock value from first item of data in the hasil array (and/or that it only ever returns one item, despite being an array).我在这里做了一个假设(等待您的评论)我们只想使用来自hasil数组中第一项数据的库存值(和/或它只返回一个项目,尽管是一个数组)。 If that's not correct then please clarify the situation with this data - it seems odd for the server to return an array if, in reality, you are only asking for information about one item.如果这不正确,那么请用此数据阐明情况 - 如果实际上您只询问有关一项的信息,服务器返回数组似乎很奇怪。 It would be more logical for it to just return a single object instead.它只返回一个对象会更合乎逻辑。


PS as a minor, unrelated point, you can also simplify this line PS作为次要的,无关的点,你也可以简化这一行

idf = (idf-1) + 2;

to

idf++;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM