ajax成功从服务器获取数据后如何触发模态隐藏。现在我的问题是,即使我在服务器端进行查询后,模式也没有关闭,这可能是我的ajax调用是错误的,但尝试了这段代码,它甚至不起作用,请参阅下面的代码。
这是值
<textarea id="scanned-QR" name="search"></textarea>
这里是模态
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
</div>
<div class="modal-body">
...
</div>
</div>
</div>
</div>
这是我从文本区域值中获取的 var 值
var query = $('#scanned-QR').val();
fetch_customer_data(query);
$(document).on('keyup', '#scanned-QR', function(){
var query = $(this).val();
fetch_customer_data(query);
});
这是我的 ajax 调用
function fetch_customer_data(query = '')
{
$.ajax({
url:"select.php",
method: 'GET',
data:{query:query},
dataType: 'json',
success:function(data) {
$("#exampleModal").removeClass("in");
$(".modal-backdrop").remove();
$('body').removeClass('modal-open');
$('body').css('padding-right', '');
$("#exampleModal").hide();
},
error:function(err){
console.log(err);
}
});
}
所以输出是:在我keyup
之后,如果textarea值是LIKE
或相同的值,则该值将与数据库值进行比较,那么如果它是相同的值。成功的ajax必须执行以下几行。
最后,我的数据库连接和我的选择查询
select.php
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$link = mysqli_connect("localhost","root","");
mysqli_select_db($link, "test");
$query = $_GET['query'];
$res = mysqli_query($link,"SELECT * FROM admin WHERE username LIKE '$admin%' ");
?>
最佳答案
There are two mistakes,
1) 将 $('#exampleModal').hide();
替换为 $('#exampleModalLong').hide();
2) 您没有从 select.php
返回 json
在 select.php
中添加此内容
if (mysqli_num_rows($res) > 0) {
$respose = array('status'=>'1');//1 for success
echo json_encode($respose );
} else {
$respose = array('status'=>'0');//0 for fail
echo json_encode($respose );
}
mysqli_close($link);
在 Ajax 中:添加此内容
success:function(data) {
if(data.status == '1'){
$("#exampleModalLong").removeClass("in");
$(".modal-backdrop").remove();
$('body').removeClass('modal-open');
$('body').css('padding-right', '');
$("#exampleModalLong").hide();
}
},
关于javascript - 进行查询选择用户名并触发模式以隐藏 - Ajax 和 Jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56249078/