【问题标题】:javascript object/array values into form inputs on click单击时将javascript对象/数组值转换为表单输入
【发布时间】:2018-08-29 17:47:44
【问题描述】:

我正在做一个自动完成功能,它用由端点查询组成的选项填充数据列表,它工作正常。但是在我的 ajax 调用结束时我遇到了一个问题,我的函数只影响数组的最后一个元素,而不是被点击的正确元素。我试图确保当用户单击列表中的选择时,它将对象的某些值发送到表单输入字段中以保存。

现在,当我单击一个选项时,它确实会将正确的对象值放入表单中,但是,它们是对应于数组中最后一个元素的值,而不是单击的那个。

我已经评论了我的问题从哪里开始,但这是整个脚本供参考。再次,列表正确填充(虽然有点慢)并且点击确实填写了表单输入,但值不对应于点击的选项,只是数组中的最后一个。

我在这里做错了什么?

<script type="text/javascript">

//If user types, send their input in ajax POST on each keystroke
$('#productInput').on('input', function(){
    if($(this).val() === ''){
       return;
    }else{

        //their input is searchResult
       const searchResult = $(this).val(); 

       $.ajax({ //url
                data: {
                    search_result:searchResult
                },
                type: "POST", 
                success: function(response){

                    //empty out old responses
                    $('#returnedProducts').empty();

                    //this starts my index
                    let searchResult = response.hits.hits;
                    for(let i = 0; i < searchResult.length; i++) {

                        //this puts matches into the datalist option, which works
                        $("#returnedProducts").append("<option value=" + searchResult[i]._source.category + ">" + searchResult[i]._source.category + "</option>");

                        /*This block is my issue*/
                        $("#productInput").on('input', function(){
                            var val = this.val = this.value;
                            if($('#returnedProducts option').filter(function(){
                                return this.value === val;
                            }).length){
                                //These elements do fill, but the values correspond to only the last array item, not the clicked one 
                                document.getElementById("grpName").value = searchResult[i]._source.frm.grp.grp_name;
                                document.getElementById("grpNum").value = searchResult[i]._source.frm.grp.grp_code;              

                            }
                        }) 
                        /*end of problem block*/ 
                    }
                }
            });
    }

});
</script>

【问题讨论】:

  • 如果你使用它而不是混入document.getElementById,你可能应该坚持使用jquery,以提高可读性等。并且也许避免使用潜在的模棱两可的陈述,例如var val = this.val = this.value;,您最终也可能会解决问题。
  • 当然,我可能会针对这些问题进行一些重构。我只是想知道我当前的问题是否与设置 [i] 而不是将其传递到我的问题函数中
  • 对于返回的每个搜索结果,您将另一个“input”事件处理函数添加到“productInput”元素。因此,如果有 5 个结果,则再创建 5 个事件处理程序。然后,当向该框中输入内容时,所有 5 个处理程序(加上原来的处理程序,因此 6 个)将同时执行。很确定这甚至与您想要的都不接近。
  • 您正在向“returnedproducts”选择列表添加选项,当用户选择一个选项时,您希望将一些数据放入表单中,对吗?也许更好地处理选择列表的“更改”事件(并在“成功”函数之外处理它一次!!),并为额外值设置选项的数据属性。当更改事件运行时,从当前选择的选项中检索数据属性并使用这些属性填充您的表单。
  • 有什么方法可以将其应用到示例中?

标签: javascript html json ajax dom-events


【解决方案1】:

问题在于,对于返回的每个搜索结果,您都需要向“productInput”元素添加另一个“input”事件处理函数。因此,如果有 5 个结果,则再创建 5 个事件处理程序。然后,当向该框中输入内容时,所有 5 个处理程序(加上原来的一个,所以 6 个)将按顺序执行。由于每次它都会覆盖相同的文本框,因此您只会看到最后一个值。我很确定这甚至与您想要的不接近。

您正在向“returnedProducts”选择列表添加选项,当用户选择一个选项时,您希望一些数据进入表单,对吗?如果是这样,处理选择列表的“更改”事件(并在“成功”函数之外处理一次!!)并在选项上设置额外值的数据属性会更有意义。当更改事件运行时,从当前选择的选项中检索数据属性并使用这些属性填充您的表单。

大致思路如下:

在你的结果循环中:

for(let i = 0; i < searchResult.length; i++) {
    //this puts matches into the select list option
    $("#returnedProducts").append("<option value='" + searchResult[i]._source.category + "' data-grpName='" + searchResult[i]._source.frm.grp.grp_name + "' data-grpNum='" + searchResult[i]._source.frm.grp.grp_code + "'>" + searchResult[i]._source.category + "</option>");
}

然后单独(完全在您的 $('#productInput').on('input', function(){ 块之外):

$("#returnedProducts").change(function() {
  var selectedOption = $(this).find("option:selected");
  $("#grpName").val(selectedOption.data("grpName"));
  $("#grpNum").val(selectedOption.data("grpNum"));
});

【讨论】:

  • 谢谢你,我相信直言不讳
猜你喜欢
  • 2021-11-15
  • 2016-10-10
  • 2017-02-14
  • 1970-01-01
  • 1970-01-01
  • 2017-07-22
  • 1970-01-01
  • 1970-01-01
  • 2017-07-22
相关资源
最近更新 更多