【问题标题】:How can I use this autofill feature from json with dynamically created inputs?如何通过动态创建的输入使用来自 json 的自动填充功能?
【发布时间】:2017-10-21 00:25:06
【问题描述】:

我正在使用这个自动填充功能,它从输入descricaoItem(description) 中获取一个值,将其发送到查询并用数据库中的codigo(code) 值填充另一个输入字段。代码如下:

$(document).ready(function() {          

            $('#descricaoItem0').change(function () {

            $.post('mandaDescricao.php',
                {
                   descricao: document.getElementById('descricaoItem0').value
                },     

                    function(data){
                        var obj = $.parseJSON(data);
                        $('#codigoItem0').val(obj.codigo); 

                    });   
                });         
        });

这个工作正常,但我想在用户按下按钮时动态创建这些输入。已经有了这个功能,并且输入是用不同的 ID(descricaoItem0、descricaoItem1 等...)创建的,这些 ID 与(codigoItem0、codigoItem1、...)相关。但是,通过动态创建这些输入,我找不到使用上述函数的方法。

你能帮帮我吗?我不想复制和粘贴这个函数直到descricaoItem20。

【问题讨论】:

  • 如果您动态创建inputs,您必须确保在创建inputs 并将其呈现到DOM 之后添加事件处理。我不能肯定这就是问题所在,但这是值得研究的问题。

标签: javascript json dynamic


【解决方案1】:

将您的事件分配器包装在一个函数中! 每次添加新输入时,触发此函数并传递正确的 ID 号。

function assignOnChangeToInput(id) {
  $('#descricaoItem' + id).change(function() {

    $.post('mandaDescricao.php', {
        descricao: document.getElementById('descricaoItem' + id).value
      },

      function(data) {
        var obj = $.parseJSON(data);
        $('#codigoItem' + id).val(obj.codigo);

      });
  });
});
}

【讨论】:

  • 这个简单的解决方案非常有效,非常感谢!
  • 我给了它,但我的声誉仍然很低,所以它不算数。
  • 好吧,到时候回来。尽可能帮助他人!
  • 会的!再次感谢:-)
【解决方案2】:
  1. 创建一个由输入对象数组组成的 JSON 对象。
[ {id: '#codigoItem0'}, {id: '#codigoItem1'}, {id: '#codigoItem2'} ]
  1. 使用 forEach 循环将更改事件侦听器应用到每个单独的节点。
var JSONObj =  [ {id: '#codigoItem0'}, {id: '#codigoItem1'}, {id: '#codigoItem2'} ]

JSONObj.forEach(function(input) {
  var id = input.id;

  $(id).change(function(e) {
    $.post('mandaDescricao.php',
      {
         descricao: document.getElementById(id).value
      },

      function(data){
        var obj = $.parseJSON(data);
        $(id).val(obj.codigo); 

      });
    });  
  })
})

可能值得制作一个更详细的 JSON 输入数组,其中输入对象可能如下所示:

{
  id: 'inputID',
  name: 'inputName'
  val: 'Foo',
  placeholder: 'Bar',
  callback: '_aCallbackMethod()'
}

然后您可以基于这些详细输入的数组创建输入元素,并将它们作为对象处理,而无需使用 $(blah) 来查询 DOM。 :-)

【讨论】:

  • 我喜欢你处理这个问题的方式,非常系统。它仍然只需要在用户向页面添加新输入时才需要执行,如问题中所述。
  • 谢谢,真的 - 在回答之前,我需要仔细阅读查询。 :-)
【解决方案3】:

您不需要使用 ID(这将有助于解决您的动态问题)您可以将事件添加到具有类的所有输入,然后在输入更改事件中使用 this 用户正在交互的输入例如

<input class="descricaoItem" />
<input class="descricaoItem" />
<input class="descricaoItem" />
<input class="descricaoItem" />
<input class="descricaoItem" />

$(document).ready(function() {

  $('.descricaoItem').on("input", function() {
    var descricaoValue = $(this).val();
    var self = this; // save context of this
    $.post('mandaDescricao.php', {
        descricao: descricaoValue
      },
      function(data) {
        var obj = $.parseJSON(data);
        $(self).val(obj.codigo);
      });
  });
});

这是一个最小的例子(没有 POST 请求)

$(document).ready(function() {
  $('.descricaoItem').on("input", function() {
    var descricaoValue = $(this).val();
    var self = this; // save context of this
    $(self).val("codigo obj: " + Math.floor(Math.random() * 100) + 1);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="descricaoItem" />
<input class="descricaoItem" />
<input class="descricaoItem" />
<input class="descricaoItem" />
<input class="descricaoItem" />

【讨论】:

    猜你喜欢
    • 2019-06-13
    • 1970-01-01
    • 1970-01-01
    • 2015-07-08
    • 2012-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-21
    相关资源
    最近更新 更多