2013-09-24 31 views
5

Bất kỳ ai có thể cho tôi biết làm cách nào để bạn kích hoạt sự kiện onselect của datepicker? Mã của tôi đang hoạt động tốt (kéo một số dữ liệu từ một cơ sở dữ liệu dựa trên giá trị của ngày mai) nhưng khi trang tải nó không kích hoạt sự kiện onselect vào ngày hiện tại và nó không hiển thị bất cứ điều gì; vì vậy tôi muốn làm điều đó một cách thủ công.Kích hoạt sự kiện bật lên ngày tháng theo cách thủ công

<script> 
    $("#datepicker").datepicker 
    (
    { 
      altField: '#sheet', 
      onSelect: function load() { 
      $(document).ready(function() { 
        $.ajax({ 
        type: 'POST', 
        url: 'test.php', 
        data: {sheetid: $('#sheet').val()}, 
        success: function(data) 
        { 
         $("#content").html(data); 
        } 
       }); 

     }); 
     }, 
      firstDay: 1}); 
    </script> 
+1

thử kích hoạt một sự kiện click cho datepicker của bạn trong 'body' 'onload' chức năng hoặc' $ (document) .ready() 'sử dụng' $ (' # datepicker ') .click(); ' – rps

Trả lời

15

Bạn có thể kích hoạt click trên datepicker ngay sau khi đặt ngày.

Code:

$(document).ready(function() { 
    $("#datepicker").datepicker({ 
     altField: '#sheet', 
     onSelect: function(date) { 
      alert(date); 
     }, 
     firstDay: 1 
    }); 

    $('#datepicker').datepicker("setDate", new Date(2013,09,22)); 
    $('.ui-datepicker-current-day').click(); // rapresent the current selected day 

}); 

Demo: http://jsfiddle.net/IrvinDominin/DynuW/

+1

Ma thuật lừa hehe, cảm ơn bạn! – Daria

0

tôi mất này từ mã nguồn của tập tin datepicker.js. Lưu ý rằng this nên được thay thế bằng phần tử đầu vào của bạn (không phải đối tượng jquery, phần tử dom thực tế). Trong trường hợp của tôi, tôi đã gọi điều này từ một sự kiện keydown.

// this is SUCH a hack. We need to call onselect to call any 
// specific validation on this input. I stole this from the datepicker source code. 
var inst = $.datepicker._getInst(this), 
onSelect = $.datepicker._get(inst, "onSelect"); 
if (onSelect) { 
    dateStr = $.datepicker._formatDate(inst); 
    onSelect.apply((inst.input ? inst.input[0] : null), [dateStr, inst]); 
} 
0

giải pháp tốt nhất

var inst = $.datepicker._getInst($("#date")[0]); 
$.datepicker._get(inst, 'onSelect').apply(inst.input[0], [$("#date").datepicker('getDate'), inst]); 
+0

Bạn có thể giải thích thêm tại sao đây là "giải pháp tốt nhất" hay ít nhất là cách hoạt động? Tôi sẽ không xem xét việc sử dụng các chức năng "ẩn" bên trong của mô-đun datepicker jQuery là một giải pháp tốt, hãy để một mình "tốt nhất". –

Các vấn đề liên quan