2016-01-29 67 views
2
<?php 
if(isset($_POST['date'])) { 
    $date = strtotime($_POST['date']); 
    $day = strtolower(date("D", $date)); 
} 
?> 
<script> 
$(function() { 
    $("#datepicker").datepicker($.extend({ 
     constrainInput: true, 
     altField: ".alternate", 
     altFormat: "yy-mm-dd", 
     minDate: 0, 
     firstDay: 1, 
     onSelect:function(dateText,instance) { 
      $.post("index.php", { date:dateText }, 
        function(data) { 
         $('#txtHint').html(''); 
         $('#txtHint').html(data); 
        }); 
     } 
    }, 
    )); 
}); 
</script> 

我想在DIV #txtHint中显示datepicker发布数据。它的工作原理,但它将整个页面的HTML张贴到这个div。我只希望发布的信息被内<div id="txtHint"></div>jQuery发布数据到div

这里显示的是index.php文件:

<?php 
$today = strtotime(date("d-m-Y")); 
$now = strtotime(date("H:i")); 
$before = "-".$settings['close_before']." hours"; 
if(isset($_POST['date'])) { 
    $date = strtotime($_POST['date']); 
    $day = strtolower(date("D", $date)); 
    // THIS HERE SHOULD BE RETURNED TO TXTHINT DIV BELOW IN FORM 
    echo "<select name=\"time\" class=\"form-control\" disabled>"; 
    echo "<option value=\"\">".$day."</option>"; 
    echo "</select>"; 
} 
?> 
<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    <title>form</title> 
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport"> 
    <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"> 
    <script src="//code.jquery.com/jquery-1.10.2.js"></script> 
    <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script> 
    <script src="js/jquery.ui.datepicker-da.js"></script> 
    <!--[if lt IE 9]> 
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script> 
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> 
    <![endif]--> 
    <script> 
    $(function() { 
     $("#datepicker").datepicker($.extend({ 
     constrainInput: true, 
     altField: ".alternate", 
     altFormat: "yy-mm-dd", 
     minDate: 0, 
     firstDay: 1, 
     onSelect:function(dateText,instance) { 
      $.post("index.php", { date:dateText }, 
      function(data) { 
       var obj = $(data); 
        $("#txtHint").html(obj.find("didHint").html()); 
       }); 
       } 
     } 
     )); 
    }); 
    </script> 
</head> 
<body> 
<form role="form" method="post" action="index.php"> 
    <div class="box-body"> 
      <div class="form-group"> 
       <div class="row"> 
        <div class="col-sm-4"> 
         <div class="input-group"> 
          <span class="input-group-addon text-bold uppercase"><i class="fa fa-calendar"></i> <small><i class="fa fa-asterisk text-red"></i></small></span> 
          <input id="datepicker" type="text" class="form-control date"> 
          <input name="date" class="alternate" type="hidden"> 
         </div> 
        </div> 
        <div class="col-sm-4"> 
         <div class="input-group"> 
          <span class="input-group-addon text-bold uppercase" style="border: none !important;"><i class="fa fa-clock-o"></i> <small><i class="fa fa-asterisk text-red"></i></small></span> 
          <!-- HERE IS WHERE POSTED SELECT SHOULD APPEAR --> 
          <div id="txtHint"> 
           <select name="time" class="form-control" disabled> 
            <option value="" selected disabled>Select date first</option> 
           </select> 
          </div> 
         </div> 
        </div> 
       </div> 
      </div> 
      <div class="form-group"> 
       <button type="submit" name="goforit" class="btn btn-success pull-right text-bold" style="text-transform: uppercase;">submit</button> 
      </div> 
     </div> 
</form> 
</body> 
</html> 

我特别希望它是在一个页面。不为外部数据调用外部脚本。 任何人都可以建议?

回答

1

更改index.php因此它在响应AJAX请求后退出,并且不打印完整的HTML文件。

<?php 
$today = strtotime(date("d-m-Y")); 
$now = strtotime(date("H:i")); 
$before = "-".$settings['close_before']." hours"; 
if(isset($_POST['date'])) { 
    $date = strtotime($_POST['date']); 
    $day = strtolower(date("D", $date)); 
    // THIS HERE SHOULD BE RETURNED TO TXTHINT DIV BELOW IN FORM 
    echo "<select name=\"time\" class=\"form-control\" disabled>"; 
    echo "<option value=\"\">".$day."</option>"; 
    echo "</select>"; 

    exit(); // <<=== Add this 
} 
?> 
+0

这并没有帮助我。它不会加载数据,就好像它没有发布一样...... –

+0

我想你需要显示'index.php'返回的内容,并解释你想要放入'#txtHint'的哪一部分。 – Barmar

+0

这是我的index.php http://pastebin.com/2zeBCC7y –