简体   繁体   English

jQuery UI自动完成功能将所选项目ID存储在输入的value属性中

[英]jQuery UI autocomplete store the selected items ID in input's value attribute

I am working on a dynamic time sheet. 我正在制定动态时间表。 My project field is using jQuery autocomplete which pulls its data from a json file. 我的项目字段使用的是jQuery自动完成功能,该功能会从json文件中提取其数据。 Once an item has been selected from autocomplete, I'm trying to set the value="" of the input field to the id of the selected item. 从自动完成中选择一个项目后,我正在尝试将输入字段的value=""设置为所选项目的id

My json file contents is like this 我的json文件内容是这样的

{
  "projectList": [{
    "label": "Some Project",
    "id": "BP1927",
    "desc": "desc1"
  }, {
    "label": "Some Other Project",
    "id": "BP1827",
    "desc": "desc1"
  }, {
    "label": "BOSS Support",
    "id": "BP6354",
    "desc": "desc3"
  }, {
    "label": "Another Support",
    "id": "BP2735"
  }]
}

What I want to do is when I select the first item Some Project , I want to show Some Project in the input-box and store the ID "BP1927" in the value attribute of the input-box. 我要做的是选择第一项Some Project ,我想在输入框中显示Some Project并将ID “ BP1927”存储在输入框的value属性中。

With my current code, my autocomplete will return project label to the input box, but when I inspect the input element, it will always show the value as being "BP2735"- which happens to be the last object within the json file. 使用我当前的代码,我的自动完成功能会将项目标签返回到输入框,但是当我检查输入元素时,它将始终显示value “ BP2735”-恰好是json文件中的最后一个对象。 What am I doing wrong? 我究竟做错了什么?

 jQuery(function($) { var rowTemplate = $('table.tablesubmit>tbody>tr:first').remove(); var autoComp = { source: function(request, response) { var regex = new RegExp(request.term, 'i'); response($.map(myJSONdata.projectList, function(item) { if (regex.test(item.id) || regex.test(item.label)) { return { label: item.label, value: item.label, id: item.id }; } $("#project").attr("value", item.id); })); } }; $('.pluslink').click(function(event) { var newRow = rowTemplate.clone(); newRow.find('input:first').autocomplete(autoComp); $('table.tablesubmit tr:last').before(newRow); }); $("table.tablesubmit").on('click', '.minuslink', function(e) { $(this).parent().parent().remove(); }); $('.pluslink').click(); //Creates the first row }); var myJSONdata = { "projectList": [{ "label": "Some Project", "id": "BP1927", "desc": "desc1" }, { "label": "Some Other Project", "id": "BP1827", "desc": "desc1" }, { "label": "BOSS Support", "id": "BP6354", "desc": "desc3" }, { "label": "Another Support", "id": "BP2735" }] } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script> <table class="tablesubmit"> <thead> <tr> <th width="30%">Project name</th> <th width="10%">Mon</th> <th width="10%">Tue</th> <th width="10%">Wed</th> <th width="10%">Thur</th> <th width="10%">Fri</th> <th width="10%">Sat</th> <th width="10%">Sun</th> <th></th> </tr> </thead> <tbody> <tr> <td> <input class="form-control full-width" id="project" type="text"> </td> <td> <input class="form-control full-width" id="full-name-f1" type="text" value="1.25"> </td> <td> <input class="form-control full-width" id="full-name-f1" type="text" value="0"> </td> <td> <input class="form-control full-width" id="full-name-f1" type="text" value="5"> </td> <td> <input class="form-control full-width" id="full-name-f1" type="text" value="1"> </td> <td> <input class="form-control full-width" id="full-name-f1" type="text" value="5.5"> </td> <td> <input class="form-control full-width" id="full-name-f1" type="text" value="0"> </td> <td> <input class="form-control full-width" id="full-name-f1" type="text" value="0"> </td> <td> <button class="btn btn-danger minuslink">Delete</button> </td> </tr> <tr> <td class="bold" width="25%"> <a>Total Time:</a> </td> <td width="10%"> <input class="form-control full-width" id="full-name-f1" type="text" value="7.25" style="background-color:#DEE0E2;"> </td> <td width="10%"> <input class="form-control full-width" id="full-name-f1" type="text" value="8" style="background-color:#DEE0E2;"> </td> <td width="10%"> <input class="form-control full-width" id="full-name-f1" type="text" value="7.5" style="background-color:#DEE0E2;"> </td> <td width="10%"> <input class="form-control full-width" id="full-name-f1" type="text" value="7" style="background-color:#DEE0E2;"> </td> <td width="10%"> <input class="form-control full-width" id="full-name-f1" type="text" value="0" style="background-color:#DEE0E2;"> </td> <td width="10%"> <input class="form-control full-width" id="full-name-f1" type="text" value="0" style="background-color:#DEE0E2;"> </td> <td width="10%"> <input class="form-control full-width" id="full-name-f1" type="text" value="0" style="background-color:#DEE0E2;"> </td> <td>37.5</td> </tr> </tbody> </table> <button class="btn btn-primary pluslink">Add new project</button> 

Updated answer as per chat discussion with OP 根据与OP进行的聊天讨论更新答案

The problem is that you are trying to set the attribute when the suggestion list is being generated. 问题在于,您正在尝试在生成建议列表时设置属性。

   source: function(request, response) {
      var regex = new RegExp(request.term, 'i');
      response($.map(myJSONdata.projectList, function(item) {
        if (regex.test(item.id) || regex.test(item.label)) {
          return {
            label: item.label,
            value: item.label,
            id: item.id
          };
        }
        $("#project").attr("value", item.id);
      }));
    }

So when the suggestion list is being generated every item is putting it's id in the value attribute and thus you are seeing the last items id while inspecting the element afterwards. 因此,在生成建议列表时,每个项目都将其ID放入value属性中,因此您在随后检查元素时会看到最后一个项目ID。

You have to set the value when a suggestion is selected, when when the suggestion list is being generated. 选择建议时,生成建议列表时,必须设置值。 The correct way to do this is to pass a input change event handler to autocomplete, that will set your attribute whenever a suggestion is selected. 正确的方法是将输入更改事件处理程序传递给自动完成功能,该功能将在选择建议时设置您的属性。

    change: function(event, ui) {
        if(ui.item){ //this checks if any value is selected
            $(event.target).attr('value',ui.item.id);
        }
    },

Jquery UI autocomplete change event documentation. jQuery UI自动完成更改事件文档。

 jQuery(function($) { var rowTemplate = $('table.tablesubmit>tbody>tr:first').remove(); var autoComp = { change: function(event, ui) { if(ui.item)$(event.target).attr('value',ui.item.id); }, source: function(request, response) { var regex = new RegExp(request.term, 'i'); response($.map(myJSONdata.projectList, function(item) { if (regex.test(item.id) || regex.test(item.label)) { return { label: item.label, value: item.label, id: item.id }; } $("#project").attr("value", item.id); })); } }; $('.pluslink').click(function(event) { var newRow = rowTemplate.clone(); newRow.find('input:first').autocomplete(autoComp); $('table.tablesubmit tr:last').before(newRow); }); $("table.tablesubmit").on('click', '.minuslink', function(e) { $(this).parent().parent().remove(); }); $('.pluslink').click(); //Creates the first row }); var myJSONdata = { "projectList": [{ "label": "Some Project", "id": "BP1927", "desc": "desc1" }, { "label": "Some Other Project", "id": "BP1827", "desc": "desc1" }, { "label": "BOSS Support", "id": "BP6354", "desc": "desc3" }, { "label": "Another Support", "id": "BP2735" }] } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css"/> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/css/bootstrap.min.css"/> <table class="tablesubmit"> <thead> <tr> <th width="30%">Project name</th> <th width="10%">Mon</th> <th width="10%">Tue</th> <th width="10%">Wed</th> <th width="10%">Thur</th> <th width="10%">Fri</th> <th width="10%">Sat</th> <th width="10%">Sun</th> <th></th> </tr> </thead> <tbody> <tr> <td> <input class="form-control full-width" id="project" type="text"> </td> <td> <input class="form-control full-width" id="full-name-f1" type="text" value="1.25"> </td> <td> <input class="form-control full-width" id="full-name-f1" type="text" value="0"> </td> <td> <input class="form-control full-width" id="full-name-f1" type="text" value="5"> </td> <td> <input class="form-control full-width" id="full-name-f1" type="text" value="1"> </td> <td> <input class="form-control full-width" id="full-name-f1" type="text" value="5.5"> </td> <td> <input class="form-control full-width" id="full-name-f1" type="text" value="0"> </td> <td> <input class="form-control full-width" id="full-name-f1" type="text" value="0"> </td> <td> <button class="btn btn-danger minuslink">Delete</button> </td> </tr> <tr> <td class="bold" width="25%"> <a>Total Time:</a> </td> <td width="10%"> <input class="form-control full-width" id="full-name-f1" type="text" value="7.25" style="background-color:#DEE0E2;"> </td> <td width="10%"> <input class="form-control full-width" id="full-name-f1" type="text" value="8" style="background-color:#DEE0E2;"> </td> <td width="10%"> <input class="form-control full-width" id="full-name-f1" type="text" value="7.5" style="background-color:#DEE0E2;"> </td> <td width="10%"> <input class="form-control full-width" id="full-name-f1" type="text" value="7" style="background-color:#DEE0E2;"> </td> <td width="10%"> <input class="form-control full-width" id="full-name-f1" type="text" value="0" style="background-color:#DEE0E2;"> </td> <td width="10%"> <input class="form-control full-width" id="full-name-f1" type="text" value="0" style="background-color:#DEE0E2;"> </td> <td width="10%"> <input class="form-control full-width" id="full-name-f1" type="text" value="0" style="background-color:#DEE0E2;"> </td> <td>37.5</td> </tr> </tbody> </table> <button class="btn btn-primary pluslink">Add new project</button> 

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

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