自动填充下拉jQuery

icomxhvb  于 2023-10-17  发布在  jQuery
关注(0)|答案(1)|浏览(138)

我想做一个自动填充国家名称的网页。当用户在可编辑类型div中键入一个字母时,屏幕将显示与用户键入的字母相匹配的字母。一旦点击任何一个标签项,它将加载到输入区域,作为带有删除X标记的标签类型项,选定的项将从标签中删除,并在删除标签时将其添加回标签中。在这里,我面临着两个问题,电子邮件不显示基于打印的信件,它显示在焦点上。第二个问题,一旦我选择了一个项目,它将作为标签添加,但不会从输入div区域删除键入的字母。请帮我解决这个问题

  1. <div class="input" contenteditable="true" id="countryInput"></div>
  2. <div class="multi-eids-dropdown" id="countryDropdown"></div>
  3. .dropdown {
  4. display: none;
  5. border: 1px solid #ccc;
  6. max-height: 100px;
  7. overflow-y: auto;
  8. }
  9. .input {
  10. border:solid 1px #ddd;
  11. }
  12. span .remove {
  13. display: inline-block;
  14. margin-left: 5px;
  15. cursor: pointer;
  16. }
  17. /*=====================================================*/
  18. .multi-eids {position: relative;}
  19. .multi-eids-dropdown { position: absolute;
  20. overflow-y: auto;
  21. top: 50px; left: 0; width: 100%;
  22. max-height: 197px; min-height: 60px;
  23. border: solid 1px #ddd;
  24. background: #fff;
  25. box-shadow: 0 0 5px #ddd;
  26. }
  27. .eid-item { display: flex; padding: 8px; border-bottom: solid 1px #ddd;}
  28. .eid-item:hover { background: var(--gray-light); }
  29. .eid-item:last-child {border-bottom: none;}
  30. .userpic { width: 32px; height: 32px; margin: 0 8px 0 0; }
  31. .eidinfos {line-height: normal;}
  32. .eidinfos p { margin: 0; font-size: 12px; font-weight: 500;}
  33. .eidinfos small {font-size: 12px; font-weight: 400;}
  34. .selected-items {
  35. /* border: 2px solid var(--grey-ui-grey-700, #b3b2b5); */
  36. height: 48px;
  37. width: 100%;
  38. top: 0;
  39. padding: 8px 0 0 0;
  40. }
  41. .selected-item {
  42. display: inline-flex;
  43. padding: 4px 8px;
  44. font-size: 12px;
  45. background: var(--gray-light);
  46. border: solid 1px #ddd;
  47. border-radius: 4px;
  48. height: 24px;
  49. margin: 0 4px 4px 0;
  50. justify-content: space-between;
  51. }
  52. .selected-item .close {margin: 0 0 0 4px; font-weight: 600; }
  53. $(document).ready(function() {
  54. const countries = ["USA", "Canada", "UK", "Australia", "France", "Germany", "Japan"];
  55. const $input = $("#countryInput");
  56. const $dropdown = $("#countryDropdown");
  57. // Populate the dropdown with country items
  58. function populateDropdown() {
  59. $dropdown.empty();
  60. countries.forEach(function(country) {
  61. if ($input.text().indexOf(country) === -1) {
  62. const $item = $("<div class='eid-item'>").text(country);
  63. $item.on("click", function() {
  64. addCountryToInput(country);
  65. $item.remove();
  66. });
  67. $dropdown.append($item);
  68. }
  69. });
  70. }
  71. function addCountryToInput(country) {
  72. const $span = $("<span contenteditable='false' class='selected-item'>").text(country);
  73. const $remove = $("<span class='close'>x</span>");
  74. $remove.on("click", function() {
  75. $span.remove();
  76. $dropdown.append($("<div class=''>").text(country).on("click", function() {
  77. addCountryToInput(country);
  78. $(this).remove();
  79. }));
  80. });
  81. $span.append($remove);
  82. $input.append($span);
  83. }
  84. // Show the dropdown when the user types a letter
  85. $input.on("input", function() {
  86. $dropdown.show();
  87. populateDropdown();
  88. });
  89. // Hide the dropdown when clicking outside the input or dropdown
  90. $(document).on("click", function(event) {
  91. if (!$input.is(event.target) && !$dropdown.is(event.target) && $dropdown.has(event.target).length === 0) {
  92. $dropdown.hide();
  93. }
  94. });
  95. });


https://jsfiddle.net/anoopsuda/pkfL30gw/16/

30byixjq

30byixjq1#

我对剧本做了一些修改。当用户单击鼠标或as开始键入时,input元素获得焦点时,新代码将显示数组中的所有项。当元素失去焦点时,隐藏对象将被隐藏。代码通过管理用户键入的字母来工作。

  1. $(document).ready(function() {
  2. const countries = ["USA", "Canada", "UK", "Australia", "France", "Germany", "Japan"];
  3. const $input = $("#countryInput");
  4. const $dropdown = $("#countryDropdown");
  5. // Populate the dropdown with country items
  6. function populateDropdown() {
  7. $dropdown.empty();
  8. const inputText = $input.text().trim();
  9. countries.forEach(function(country) {
  10. if (country.toLowerCase().includes(inputText.toLowerCase())) {
  11. const $item = $("<div class='eid-item'>").text(country);
  12. $item.on("click", function() {
  13. addCountryToInput(country);
  14. $item.remove();
  15. });
  16. $dropdown.append($item);
  17. }
  18. });
  19. }
  20. function addCountryToInput(country) {
  21. const $span = $("<span contenteditable='false' class='selected-item'>").text(country);
  22. const $remove = $("<span class='close'>x</span");
  23. $remove.on("click", function() {
  24. $span.remove();
  25. $dropdown.append($("<div class='eid-item'>").text(country).on("click", function() {
  26. addCountryToInput(country);
  27. $(this).remove();
  28. }));
  29. });
  30. $span.append($remove);
  31. $input.empty();
  32. $input.append($span);
  33. }
  34. // Show the dropdown when the user types a letter or clicks on the input element
  35. $input.on("input click", function() {
  36. $dropdown.show();
  37. populateDropdown();
  38. });
  39. // Hide the dropdown when clicking outside the input or dropdown
  40. $(document).on("click", function(event) {
  41. if (!$input.is(event.target) && !$dropdown.is(event.target) && $dropdown.has(event.target).length === 0) {
  42. $dropdown.hide();
  43. }
  44. });
  45. });
  1. .dropdown {
  2. display: none;
  3. border: 1px solid #ccc;
  4. max-height: 100px;
  5. overflow-y: auto;
  6. }
  7. .input {
  8. border: solid 1px #ddd;
  9. }
  10. span .remove {
  11. display: inline-block;
  12. margin-left: 5px;
  13. cursor: pointer;
  14. }
  15. /*=====================================================*/
  16. .multi-eids {
  17. position: relative;
  18. }
  19. .multi-eids-dropdown {
  20. position: absolute;
  21. overflow-y: auto;
  22. top: 50px;
  23. left: 0;
  24. width: 100%;
  25. max-height: 197px;
  26. min-height: 60px;
  27. border: solid 1px #ddd;
  28. background: #fff;
  29. box-shadow: 0 0 5px #ddd;
  30. }
  31. .eid-item {
  32. display: flex;
  33. padding: 8px;
  34. border-bottom: solid 1px #ddd;
  35. }
  36. .eid-item:hover {
  37. background: var(--gray-light);
  38. }
  39. .eid-item:last-child {
  40. border-bottom: none;
  41. }
  42. .userpic {
  43. width: 32px;
  44. height: 32px;
  45. margin: 0 8px 0 0;
  46. }
  47. .eidinfos {
  48. line-height: normal;
  49. }
  50. .eidinfos p {
  51. margin: 0;
  52. font-size: 12px;
  53. font-weight: 500;
  54. }
  55. .eidinfos small {
  56. font-size: 12px;
  57. font-weight: 400;
  58. }
  59. .selected-items {
  60. /* border: 2px solid var(--grey-ui-grey-700, #b3b2b5); */
  61. height: 48px;
  62. width: 100%;
  63. top: 0;
  64. padding: 8px 0 0 0;
  65. }
  66. .selected-item {
  67. display: inline-flex;
  68. padding: 4px 8px;
  69. font-size: 12px;
  70. background: var(--gray-light);
  71. border: solid 1px #ddd;
  72. border-radius: 4px;
  73. height: 24px;
  74. margin: 0 4px 4px 0;
  75. justify-content: space-between;
  76. }
  77. .selected-item .close {
  78. margin: 0 0 0 4px;
  79. font-weight: 600;
  80. }
  1. <div class="input" contenteditable="true" id="countryInput"></div>
  2. <div class="multi-eids-dropdown" id="countryDropdown"></div>
  3. <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

希望符合你的要求

展开查看全部

相关问题