gpt4 book ai didi

javascript - AJAX jquery 没有响应并显示 div

转载 作者:太空宇宙 更新时间:2023-11-04 15:57:51 26 4
gpt4 key购买 nike

我编写了一个简单的表单代码,我可以在其中动态检索数据,然后将其发送到另一个页面。使用该数据,我希望在我的页面上显示一些 div。当我在不使用 AJAX 的情况下简单地检查它时,它正在返回 div。但是现在我已经应用了一些 AJAX,但它不起作用。请提出任何建议。

AJAX

$("document").ready(function() {
$("#search_keyword").on("submit", function (e) {
e.preventDefault();
$.post("keyword_search.php?query="+encodeURIComponent($("#keyword").val())+"category="+encodeURIComponent($("#category").val())+"store="+encodeURIComponent($("#store").val()), function (data) {
var res = JSON.parse(data);
if (res.divs) {
$('#search_result').html("");
for (var i = 0; i < res.divs.length; i++) {
$('#search_result').append(res.divs[i]);
}
} else {
$('#search_result').html("No matched coupons found !");
}
});
});
});

表单

<form class="form-horizontal select-search" id="search_keyword" method="post">
<label class="control-label ">Keyword</label>
<input class="form-control" id="keyword" name="keyword" type="text">
<!-- Select Category -->
<label class="control-label " for="category">Select category</label>
<select class="category" id="category" name="category">
<?php
$sm=mysqli_query($con,"select * from categories ");
while ($row1 = mysqli_fetch_array($sm,MYSQLI_ASSOC)){
$cat_id = $row1['cat_id'];
$name = $row1['cat_name'];
echo '<option value="' . $cat_id . '">' . $name . '</option>';
}
?>
</select>

<label class="control-label " for="store">Select a store</label>
<select class="storesname" id="store" name="store">
<option selected="selected">Select Stores</option>
</select>
<button id="search_btn" name="search_btn" class="btn btn-danger">Search coupons</button>
</form>

<div id="search_result"> </div>

最佳答案

您需要从 button 更改至 submit键入以便它可以实际提交。

所以改变:-

<button id="search_btn" name="search_btn" class="btn btn-danger">Search coupons</button>

致:-

<input type="submit" id="search_btn" name="search_btn" class="btn btn-danger" value="Search coupons"/>

注意:- 确保 jQuery在您的脚本代码之前添加库,以便它可以工作。

像下面这样更改您的代码:-

$("document").ready(function() {
$("#search_keyword").on("submit", function (e) {
e.preventDefault();
var data = {'query':encodeURIComponent($("#keyword").val()),'category':encodeURIComponent($("#category").val()),'store':encodeURIComponent($("#store").val())};
$.post("keyword_search.php",data, function (data) {
var res = JSON.parse(data);
if (res.divs) {
$('#search_result').html("");
for (var i = 0; i < res.divs.length; i++) {
$('#search_result').append(res.divs[i]);
}
} else {
$('#search_result').html("No matched coupons found !");
}
});
});
});

在你的keyword_search.php像这样检查:-

<?php
echo "<pre/>";print_r($_POST); //check that how post data are coming
// rest do code accordingly
?>

同时删除 method="post"来自您当前的 <form>

关于javascript - AJAX jquery 没有响应并显示 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44132378/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com