ວິທີການສ້າງການເຮັດການຄົ້ນຫາອັດຕາ
ຮຽນວິທີການສ້າງການເຮັດການຄົ້ນຫາອັດຕາ
ການເຮັດການຄົ້ນຫາອັດຕາ
ການເລີ່ມບັນທືກ:
ສ້າງຟອມຂໍ້ມູນການເຮັດການຄົ້ນຫາອັດຕາ
ບາງທີ 1 - ສ້າງ HTML:
<!-- ຮັບປະກັນວ່າຟອມຂໍ້ມູນບໍ່ມີການປິດການການເຮັດການຄົ້ນຫາອີກ --> <form autocomplete="off" action="/action_page.php"> <div class="autocomplete" style="width:300px;"> <input id="myInput" type="text" name="myCountry" placeholder="Country"> </div> <input type="submit"> </form>
ບາງທີ 2 - ສ້າງອັດຕາ JavaScript:
ສະແດງວຽກກັນທັງໝົດຂອງປະເທດ/ເຂດທົ່ວໂລກ:
var countries = ["Afghanistan","Albania","Algeria","Andorra","Angola","Anguilla","Antigua & Barbuda","Argentina","Armenia","Aruba","Australia","Austria","Azerbaijan","Bahamas","Bahrain","Bangladesh","Barbados","Belarus","Belgium","Belize","Benin","Bermuda","Bhutan","Bolivia","Bosnia & Herzegovina","Botswana","Brazil","British Virgin Islands","Brunei","Bulgaria","Burkina Faso","Burundi","Cambodia","Cameroon","Canada","Cape Verde","Cayman Islands","Central Arfrican Republic","Chad","Chile","China","Colombia","Congo","Cook Islands","Costa Rica","Cote D Ivoire","Croatia","Cuba","Curacao","Cyprus","Czech Republic","Denmark","Djibouti","Dominica","Dominican Republic","Ecuador","Egypt","El Salvador","Equatorial Guinea","Eritrea","Estonia","Ethiopia","Falkland Islands","Faroe Islands","Fiji","Finland","France","French Polynesia","French West Indies","Gabon","Gambia","Georgia","Germany","Ghana","Gibraltar","Greece","Greenland","Grenada","Guam","Guatemala","Guernsey","Guinea","Guinea Bissau","Guyana","Haiti","Honduras","Hong Kong","Hungary","Iceland","India","Indonesia","Iran","Iraq","Ireland","Isle of Man","Israel","Italy","Jamaica","Japan","Jersey","Jordan","Kazakhstan","Kenya","Kiribati","Kosovo","Kuwait","Kyrgyzstan","Laos","Latvia","Lebanon","Lesotho","Liberia","Libya","Liechtenstein","Lithuania","Luxembourg","Macau","Macedonia","Madagascar","Malawi","Malaysia","Maldives","Mali","Malta","Marshall Islands","Mauritania","Mauritius","Mexico","Micronesia","Moldova","Monaco","Mongolia","Montenegro","Montserrat","Morocco","Mozambique","Myanmar","Namibia","Nauro","Nepal","Netherlands","Netherlands Antilles","New Caledonia","New Zealand","Nicaragua","Niger","Nigeria","North Korea","Norway","Oman","Pakistan","Palau","Palestine","Panama","Papua New Guinea","Paraguay","Peru","Philippines","Poland","Portugal","Puerto Rico","Qatar","Reunion","Romania","Russia","Rwanda","Saint Pierre & Miquelon","Samoa","San Marino","Sao Tome and Principe","Saudi Arabia","Senegal","Serbia","Seychelles","Sierra Leone","Singapore","Slovakia","Slovenia","Solomon Islands","Somalia","South Africa","South Korea","South Sudan","Spain","Sri Lanka","St Kitts & Nevis","St Lucia","St Vincent","Sudan","Suriname","Swaziland","Sweden","Switzerland","Syria","Taiwan","Tajikistan","Tanzania","Thailand","Timor L'Este","Togo","Tonga","Trinidad & Tobago","Tunisia","Turkey","Turkmenistan","Turks & Caicos","Tuvalu","Uganda","Ukraine","United Arab Emirates","United Kingdom","United States of America","Uruguay","Uzbekistan","Vanuatu","Vatican City","Venezuela","Vietnam","Virgin Islands (US)","Yemen","Zambia","Zimbabwe"];
ບາງການທີ3 - ການເພີ່ມ CSS:
ບັນຊີຕ້ອງມີການຕິດຕັ້ງທາງສົງ.
* { box-sizing: border-box; } body { font: 16px Arial; } .autocomplete { /*ບັນຊີຕ້ອງຖືກຕິດຕັ້ງທາງສົງ: */ position: relative; display: inline-block; } input { border: 1px solid transparent; background-color: #f1f1f1; padding: 10px; font-size: 16px; } input[type=text] { background-color: #f1f1f1; width: 100%; } input[type=submit] { background-color: DodgerBlue; color: #fff; } .autocomplete-items { position: absolute; border: 1px solid #d4d4d4; border-bottom: none; border-top: none; z-index: 99; /* ການຕິດຕັ້ງປະຕິບັດສຳລັບການຕິດຕັ້ງຄວາມຍາວຂອງສະນະພາບກັບບັນຊີ: */ top: 100%; left: 0; right: 0; } .autocomplete-items div { padding: 10px; cursor: pointer; background-color: #fff; border-bottom: 1px solid #d4d4d4; } .autocomplete-items div:hover { /* ການດຳເນີນຕາມຫຼັງທີ່ຄົນກຳລັງຂຽນໃນບາງຢ່າງ: */ background-color: #e9e9e9; } .autocomplete-active { /* ການດຳເນີນຕາມຫຼັງທີ່ຄົນກຳລັງພົບບາງຢ່າງທີ່ຈະເລີ່ມຕົ້ນດ້ວຍອອກຫຼຽນ: */ background-color: DodgerBlue !important; color: #ffffff; }
ການທີ4 - ການເພີ່ມ JavaScript:
function autocomplete(inp, arr) { /* ການດຳເນີນຕາມຫຼັງທີ່ຄົນກຳລັງຂຽນໃນບາງຢ່າງ: */ var currentFocus; /* ການດຳເນີນຕາມຫຼັງທີ່ຄົນກຳລັງຂຽນໃນບາງຢ່າງ: */ inp.addEventListener("input", function(e) { var a, b, i, val = this.value; /* ການປິດບັນດາລາຍການຄົນນັ້ນທີ່ຈະເປີດ: */ closeAllLists(); if (!val) { return false;} currentFocus = -1; /* ການສ້າງປະກອບສາຍດາວ DIV ທີ່ບັນຈຸບາງຢ່າງ: */ a = document.createElement("DIV"); a.setAttribute("id", this.id + "autocomplete-list"); a.setAttribute("class", "autocomplete-items"); /* ການເພີ່ມປະກອບສາຍດາວ DIV ໃຫ້ເປັນລູກສາຍຂອງຄອນເຕີໂລຂອງຄອນເຕີໂລຄົນນັ້ນ: */ this.parentNode.appendChild(a); /* ການອອກແບບການປິ່ນປົວບັນດາບາງຢ່າງໃນອາກາດ: */ for (i = 0; i < arr.length; i++) { /* ການກວດສອບວ່າບັນດາບາງຢ່າງຈະເລີ່ມຕົ້ນດ້ວຍສີບາງຄຳທີ່ຕາມຄວາມຂອງບາງຢ່າງ: */ if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase()) { /* ສ້າງຫນັງທີ່ຈະຫນັງວັດຖຸຄິດຄອງທີ່ຈະຫນັງ: */ b = document.createElement("DIV"); /* ຂຽນຫນັງປະກອບທີ່ຕິດຕາມມັນຄິດຄອງ: */ b.innerHTML = "<strong>" + arr[i].substr(0, val.length) + "</strong>"; b.innerHTML += arr[i].substr(val.length); /* ລົງຄວາມຄິດຄອງວັດຖຸຄິດຄອງຫນັງຫນັງວັດຖຸຄິດຄອງ: */ b.innerHTML += "<input type='hidden' value='" + arr[i] + "'>"; /* ຖ້າມີຄົນລະເບີດຫນັງວັດຖຸຄິດຄອງທີ່ຈະດຳເນີນຕາມຫນັງທີ່: */ b.addEventListener("click", function(e) { /* ລົງຄວາມຄິດຄອງຫນັງວັດຖຸຄິດຄອງຄິດຄອງ: */ inp.value = this.getElementsByTagName("input")[0].value; /*ປິດບັນທຶກວັດຖຸຄວາມຄິດຄອງຫນັງທີ່ປິດບັນທຶກຫນັງຫນັງຫນັງຫນັງ: */ closeAllLists(); }); a.appendChild(b); } } }); /* ຖ້າມີຄົນປະຕິບັດຄຳສັ່ງຄຳຂອງຄອນພິມບົດກະບາສິດຫນັງຈະດຳເນີນຕາມຫນັງທີ່: */ inp.addEventListener("keydown", function(e) { var x = document.getElementById(this.id + "autocomplete-list"); if (x) x = x.getElementsByTagName("div"); if (e.keyCode == 40) { /*ຖ້າຫວັງຄັກດັບປະຕູມິນຕິນລຸກຫນັງຈະເພີ່ມປະກອບຄູ່ currentFocus: */ currentFocus++; /* ແລະເຮັດໃຫ້ວັດຖຸປະຈຸບັນຈະຈະເປັນຫນັງທີ່ຊັດເຈນຫນັງ: */ addActive(x); } else if (e.keyCode == 38) { //up /* ຖ້າຫວັງຄັກດັບປະຕູມິນຕິນກ່ວາຄົງອີກຈະຫຼຸດປະກອບຄູ່ currentFocus: */ currentFocus--; /* ແລະເຮັດໃຫ້ວັດຖຸປະຈຸບັນຈະຈະເປັນຫນັງທີ່ຊັດເຈນຫນັງ: */ addActive(x); } else if (e.keyCode == 13) { /* ຖ້າຫວັງຄັກ ENTER ຂາດຈະຢື່ນອອກຫນັງບັນລາສະໜອງ: */ e.preventDefault(); if (currentFocus > -1) { /* ແລະການດຳເນີນການຄັດຄະແນນວັດສະດຸ"active": */ if (x) x[currentFocus].click(); } } }); function addActive(x) { /* ການຈັດການລະບົບຂອງວັດສະດຸທີ່ມີ "active": */ if (!x) return false; /* ການລົບ "active" ຄັນທັງໝົດຈາກທຸກລາຍການ: */ removeActive(x); if (currentFocus >= x.length) currentFocus = 0; if (currentFocus < 0) currentFocus = (x.length - 1); /* ການເພີ່ມ "autocomplete-active" ຄັນ: */ x[currentFocus].classList.add("autocomplete-active"); } function removeActive(x) { /* ການລົບ "autocomplete-active" ຄັນຈາກຜູ້ອອກຄຳສັ່ງ: */ for (var i = 0; i < x.length; i++) { x[i].classList.remove("autocomplete-active"); } } function closeAllLists(elmnt) { /* ປິດລະບົບສະເໜີອີກອັນທັງໝົດໃນເອກະສານແບບອື່ນໆ: */ var x = document.getElementsByClassName("autocomplete-items"); for (var i = 0; i < x.length; i++) { if (elmnt != x[i] && elmnt != inp) { x[i].parentNode.removeChild(x[i]); } } } /* ການດຳເນີນຕາມການຄັດຄະແນນໂດຍຜູ້ຄົນໃນເອກະສານ */ document.addEventListener("click", function (e) { closeAllLists(e.target); }); }
ການກຳນົດບັນຫາທີ5 - ສະຫຼຸບການສະເໜີອີກອັນໂດຍ "myInput":
ຂະຫຍາຍລະບົບປະເທດຫຼາຍຫຼາຍຫຼາຍ autocomplete
ການສົ່ງຂໍ້ມູນຄັນທີສອງ:
<script> autocomplete(document.getElementById("myInput"), countries);