如何与ajax livesearch与datepicker关联

时间:2018-12-08 13:19:23

标签: javascript ajax datepicker livesearch

我有问题,如果您能帮助,我会很高兴。我想在网站的搜索框中同时使用datepicker和ajax livesearch。在搜索框中手动输入日期后,会将其添加到搜索框中,并结束搜索。如果我从日期选择器中选择日期,则该日期会显示在搜索框中,但实时搜索无效或无法搜索。据我了解,实时搜索未处理datepicker的结果。我想我需要一个JavaScript来获取彼此的数据,但是我不能。我在等你的帮助。以下代码是相关的。谢谢。

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.7.1/css/bootstrap-datepicker.min.css" rel="stylesheet"/>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
	
<script>
function showResult(str) {
  if (str.length==0) { 
    document.getElementById("livesearch").innerHTML="";
    document.getElementById("livesearch").style.border="0px";
    return;
  }
  if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  } else {  // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange=function() {
    if (this.readyState==4 && this.status==200) {
      document.getElementById("livesearch").innerHTML=this.responseText;
      document.getElementById("livesearch").style.border="1px solid #A5ACB2";
    }
  }
  xmlhttp.open("GET","livesearch.php?q="+str,true);
  xmlhttp.send();
}
</script>
</head>
	<body>

<div class="container">
    <div class="row" style="padding-top: 10px">
        <div class="col">
            <input type="text" size="30" onkeyup="showResult(this.value)" id="datepicker">
        </div>
		<div id="livesearch"></div>
    </div>
 </div>
		

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.7.1/js/bootstrap-datepicker.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script type="text/javascript">
    $('#datepicker').datepicker({
        weekStart: 1,
        daysOfWeekHighlighted: "6,0",
        autoclose: true,
        todayHighlight: true,
		format: "dd.mm.yyyy",
		clearBtn: true,
        language:  'tr',
        todayBtn:  true,
		autoclose: false,
		dayofweekhiglight: true,
    });
    $('#datepicker').datepicker("setDate", new Date());
	

	
</script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

引导程序日期选择器具有changeDate事件,可用于调用showResult函数。参见https://bootstrap-datepicker.readthedocs.io/en/latest/events.html#changedate