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

标签 javascript jquery json jquery-ui jquery-ui-autocomplete

我正在制作动态时间表。我的项目字段使用 jQuery 自动完成,它从 json 文件中提取数据。从自动完成中选择一个项目后,我尝试将输入字段的 value="" 设置为所选项目的 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"
  }]
}

我想要做的是,当我选择第一个项目Some Project时,我想在输入框中显示Some Project并存储ID“BP1927”在输入框的 value 属性中。

使用我当前的代码,我的自动完成功能会将项目标签返回到输入框,但是当我检查输入元素时,它总是将 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>

关于javascript - jQuery UI 自动完成将所选项目 ID 存储在输入的 value 属性中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48641995/

相关文章:

javascript - 如何在我的编辑器中使用灰色/棕褐色裁剪图像。?

javascript - 通过点击 html 链接在谷歌地图上显示信息窗口

javascript - 使用 ng-blur 进行输入验证(如果输入错误则更改背景颜色)

javascript - c# mvc nreco HtmlToPdfConverter 问题将 html 文档转换为 pdf

javascript - 如何使用指定的键码触发类似 keyup 的事件

Javascript - 如何删除单词之间的所有额外间距

python - 访问 Python 字典中的 Unicode 值

json - QR 码限制

json - 如何插入 Postgresql JSON 数组

javascript - 在另一个页面上调用 javascript 函数/按钮单击事件