我正在嘗試制作一個搜索欄,從輸入的信件中發(fā)送提案請求。jQuery 發(fā)送的請求數(shù)與輸入的字母數(shù)相同。如果我寫“sa”,jQuery 將發(fā)送 2 個請求。如果我寫“Sa”,jQuery 將發(fā)送 3 個請求(因為我按下了 shift,所以大字母被算作 2 個字母)。代碼應(yīng)該像以下說明一樣工作: 用戶寫下他想要查找的名稱和數(shù)據(jù)庫中的代碼搜索,并顯示所有可能的搜索詞。 編輯: 腳本應(yīng)發(fā)送 2 個請求:第一個獲取具有“sa”的所有結(jié)果。選擇結(jié)果后。它應(yīng)該只向數(shù)據(jù)庫發(fā)送一個請求以獲取其余信息。我的腳本正在做什么:他正在發(fā)送他應(yīng)該做的第一個請求。第二個請求是在與字母數(shù)量相同的時間發(fā)送。即使只有一個請求就足夠了這是我的 jQuery 代碼:$('.sucherInput').on('keyup', function () {// ** .sucherInput is the class of the input fieldvar inputValue = $('.sucherInput').val();var result = $(".sucherres");var resultList = $(".sucherres ul");if (inputValue.length) { console.log(inputValue); $.post("action/php/artikelSucher.php", { name: inputValue }).done(function(data){ // Display the returned data in browser //console.log(data); resultList.html(data); result.show(); }); //.done} else{ console.log("empty"); resultList.empty(); result.hide();}$(document).on("click", ".sucherres ul li p", function(){ //set the search bar value as same as the clicked <p> tag $('.sucherInput').val($(this).text()); //clear the Proposals list $(resultList).empty(); $(result).hide(); //renew the value of the search bar //since im taking the value which have to be searched in the database from the searchbar var sucherLastIndex = $('.sucherInput').val(); console.log("Getting data from database: " + sucherLastIndex); //load the data into the HTML file $("#updateDiv #info").load("action/php/index-preis-liste.php", { name: sucherLastIndex });});});這是 Html 代碼:<div class="sucher"> <ul style="width: 100%; margin-bottom: 0;"> <li style="width: 33%;"> <input type="text" class="sucherInput" placeholder="Geben Sie einen Suchbegriff ein"> </li> </ul> <div class="sucherres"> <ul> <!-- ## HERE COMES THE Proposals ## --> </ul> </div> <div id="info"> </div>
Jquery 正在發(fā)送多個請求
九州編程
2021-11-04 15:58:58