为了账号安全,请及时绑定邮箱和手机立即绑定

使查询选择像用户名并触发模式隐藏 - Ajax & Jquery

使查询选择像用户名并触发模式隐藏 - Ajax & Jquery

PHP
手掌心 2021-06-29 14:40:12
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>这是我从 textarea 值中获取的 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必须执行以下几行。最后,我的数据库连接和我的选择查询选择.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%' ");  ?>
查看完整描述

1 回答

?
慕雪6442864

TA贡献1812条经验 获得超5个赞

有两个错误,


1) 替换$('#exampleModal').hide();为$('#exampleModalLong').hide();


2)你不是json从你的select.php


在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();

   }

},


查看完整回答
反对 回复 2021-07-02
  • 1 回答
  • 0 关注
  • 105 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信