2013-02-21 57 views
5

大家下午好如何在datepicker更改日期時使用ajax post?

輸入值隨着datepicker ui的變化。

我想要使用ajax後輸入值將會改變。

爲此,我使用的腳本:

<input type="text" name="date" class="datepicker" id="datepicker"> 

$(".datepicker").datepicker({changeYear: true}); 

$(".datepicker").on("change",function(){ 
var date=$(this).val(); 
$.post("test.php", { 
date:date 
}, 
function(data){$('#testdiv').html('');$('#testdiv').html(data); 
}); 
}); 

但阿賈克斯後查詢與舊的日期執行。

Datepicker在輸入中沒有時間變化值。

告訴我如何在datepicker更改字段中的日期後執行查詢?

我需要:

1)datepicker更改日期;

2)查詢應該用新的日期發送。

回答

10

您可以在日期選擇器的事件觸發AJAX後:

$("#datepicker").datepicker({ 

    onSelect: function(date, instance) { 

     $.ajax 
     ({ 
       type: "Post", 
       url: "www.example.com", 
       data: "date="+date, 
       success: function(result) 
       { 
        //do something 
       } 
     }); 
    } 
}); 

希望這helps- @Codebrain

+0

感謝你的答案)代碼 – 2013-02-21 07:13:11

+0

歡迎@LeoLoki – Sakthivel 2013-02-21 08:19:00

2

也許你應該考慮使用日期選擇器的onSelect事件(見here)。

使用該事件,新選擇的日期(作爲字符串)作爲第一個參數傳遞。

+0

@andi感謝) – 2013-02-21 07:12:22

1

而不是使用on(change你有onSelect datepicker,它會在日期改變時觸發。

$(function(){ 
    $('.datepicker').datepicker({ 
     onSelect:function(dateText,instance){ 
      alert(dateText); //Latest selected date will give the alert. 
      $.post("test.php", { 
      date:dateText // now you will get the selected date to `date` in your post 
      }, 
      function(data){$('#testdiv').html('');$('#testdiv').html(data); 
      }); 
     } 
    }); 
}); 

希望它有幫助。

相關問題