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

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

我在动态时间工作sheet。我的项目字段正在使用 jQuery 自动完成功能,它从 json 文件中提取数据。从自动完成 selected 项目后,我试图将输入字段的 value="" 设置为 selected 项目的 id

我的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"
  }]
}

我想做的是当我 select 第一项 Some Project 时,我想在输入框中显示 Some Project 并存储 ID "BP1927" 在输入框的值属性中。

使用我当前的代码,我的自动完成会将 return 项目标签投射到输入框,但是当我检查输入元素时,它将始终显示 value 为 "BP2735" - 这恰好是 json 文件中的最后一个对象。我做错了什么?

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>

根据与 OP

的聊天讨论更新了答案

问题是您试图在生成建议列表时设置属性。

   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);
      }));
    }

因此,在生成建议列表时,每个项目都会将其 ID 放入 value 属性中,因此您在之后检查元素时会看到最后一个项目 ID。

您必须在选择建议时、在生成建议列表时设置值。执行此操作的正确方法是将输入更改事件处理程序传递给自动完成,这将在选择建议时设置您的属性。

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

Jquery UI 自动完成 change event documentation.

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>