2013-02-18 31 views
0

我一直在試圖讓最後幾天得到這個東西的工作。 有兩個日期選擇器很像開始和結束日期演示,但不能讓它們工作。此代碼適用於頁面正文,但不適用於我希望擁有搜索欄的標題。JQuery日期選擇器行爲

  <script type="text/javascript"> 
     $(function() { 
      $("#from").datepicker({ 
       dateFormat: "dd-mm-yy", 
       defaultDate: "+1w", 
       changeMonth: true, 
       numberOfMonths: 3, 
       onClose: function(selectedDate) { 
        $("#to").datepicker("option", "minDate", selectedDate); 
       } 
      }); 
      $("#to").datepicker({ 
       dateFormat: "dd-mm-yy", 
       defaultDate: "+1w", 
       changeMonth: true, 
       numberOfMonths: 3, 
       onClose: function(selectedDate) { 
        $("#from").datepicker("option", "maxDate", selectedDate); 
       } 
      }); 
     }); 
    </script> 
    <form method="post" name="top_search_form" action="<?php bloginfo('wpurl');?>/?page_id=13" style="z-index:inherit"> 
     <input type="hidden" name="a" value="dosearch"/> 
     <input id="top_search_input" placeholder="Procurar..." style="z-index:inherit" type="text" name="keywordphrase" value=""/> 
      <input type="text" id="from" name="searchstartdate" placeholder="Partida" class="from" style="z-index:inherit"/> 
     <input type="text" id="to" name="searchenddate" placeholder="Chegada" class="to" style="z-index:inherit"/> 
     <input id="top_search_button" type="submit" value="Search" style="z-index:inherit"> 
    </form> 

任何人都可以幫助我嗎?

+2

它是如何工作的?怎麼了 ?你得到什麼錯誤? – ManseUK 2013-02-18 11:32:30

+1

您是否使用在兩處複製相同ID的相同表格? – ryadavilli 2013-02-18 11:33:30

+0

它給出了在ID或類的衝突錯誤,所以看看 – sasi 2013-02-18 11:34:17

回答

0

由於某些原因,日期選擇器不喜歡將函數移動到正在使用的函數()外,/未正確掛接到框。

下面是我如何做,它工作正常。

Start : <input type="text" name="startdate" id="startdate" value="<?php echo $startdate;?>" /> 
End : <input type="text" name="enddate" id="enddate" value="<?php echo $enddate;?>" /> 

<script> 
//date popup instigation 
$("#startdate").datepicker({ dateFormat: 'yy-mm-dd', changeMonth: true, changeYear: true }); 
$("#startdate").datepicker("option", "dateFormat", "yy-mm-dd"); 
$("#startdate").datepicker("option", "showAnim", "blind"); 
$("#enddate").datepicker({ dateFormat: 'yy-mm-dd', changeMonth: true, changeYear: true }); 
$("#enddate").datepicker("option", "dateFormat", "yy-mm-dd"); 
$("#enddate").datepicker("option", "showAnim", "blind"); 
</script> 
+0

調用'datetimepicker'需要額外的插件:) – Morpheus 2013-02-18 12:10:00

+0

它完全相同的插件只是爲了支持時間而不是日期。將它重命名爲.datepicker,它的功能完全相同,除了它的主要問題仍然是它的function()包裝器內部! – Dave 2013-02-18 12:16:30

+1

我知道:)我在你的回答中提到了這個問題。 – Morpheus 2013-02-18 12:17:53

0

可能是你的頁眉有不同的php頁面。所以在你的頭文件中包含.js文件和datepicker的代碼。希望它能爲你工作。

+0

是的,我已經做到了! 但是仍然不起作用... – rk22 2013-02-18 11:45:22

+0

實際上會出現什麼錯誤? – ripa 2013-02-18 11:48:18

0

我會使用類而不是id的,我也將放置Javascript和文檔的結尾,這應該解決您的問題。

<form method="post" name="top_search_form" action="<?php bloginfo('wpurl');?>/?page_id=13" style="z-index:inherit"> 
    <input type="hidden" name="a" value="dosearch"/> 
    <input id="top_search_input" placeholder="Procurar..." style="z-index:inherit" type="text" name="keywordphrase" value=""/> 
    <input type="text" id="from" name="searchstartdate" placeholder="Partida" class="from" style="z-index:inherit"/> 
    <input type="text" id="to" name="searchenddate" placeholder="Chegada" class="to" style="z-index:inherit"/> 
    <input id="top_search_button" type="submit" value="Search" style="z-index:inherit"> 
</form> 
<script type="text/javascript"> 
$(document).ready({ 
    $(".from").datepicker({ 
     dateFormat: "dd-mm-yy", 
     defaultDate: "+1w", 
     changeMonth: true, 
     numberOfMonths: 3, 
     onClose: function(selectedDate) { 
      $(".to").datepicker("option", "minDate", selectedDate); 
     } 
    }); 
    $(".to").datepicker({ 
     dateFormat: "dd-mm-yy", 
     defaultDate: "+1w", 
     changeMonth: true, 
     numberOfMonths: 3, 
     onClose: function(selectedDate) { 
      $(".from").datepicker("option", "maxDate", selectedDate); 
     } 
    }); 
}); 
</script>