2013-08-21 148 views
0

我遇到了ajax沒有發佈預期數據的問題, 我正在使用Codeigniter日曆庫,並向每個Table Row和Table Data標記添加了類 ,以便當您單擊任何表格單元格下面的代碼將觸發。 不知道是怎麼了?從jQuery Ajax獲取發佈數據

這裏是代碼:

<script type="text/javascript"> 

$('.calendar .day').click(function(){ 
    get_day_num(); 
}); 

function get_day_num() 
{ 
    //this alert shows 'undefined' ?? 
    alert($(this).find('.day').html()); 
    //values being passed to sendValue are probably 'undefined' - why?   
    sendValue($(this).find('.day_num').html(),$(this).find('.content').html()) 
} 

function sendValue(day_num,day_data) 
{ 
    $.ajax({ 
    url: window.location, 
    type: 'POST', 
    data: { 
    day: day_num, 
    data: day_data 
    }, 
    complete: function(msg) 
    { 
    location:reload(); 
    } 
}); 
} 
</script> 

回答

1

假設script標籤是在你的頁面的head,你需要用你的代碼在文檔準備好處理程序。

<script type="text/javascript"> 
    $(function() { 
     // your code here... 
    }); 
</script> 

此外,thisget_day_num功能將其點擊的window,而不是元素。您需要將該元素傳遞給函數,或者使用函數引用作爲處理程序。試試這個:

$('.calendar .day').click(get_day_num); 
+0

拍,我錯過了! – Mystx

+0

我jQuery代碼是在頁面的主體。這可能是爲什麼它不工作? – Mystx

+0

它是在身體的結尾,''之前吧? –

0
function sendValue(day_num,day_data) 
{ 
    $.ajax({ 
    url: window.location, 
    type: 'POST', 
    data: { 
    day: day_num, 
    data: day_data 
    }, 
    complete: function(msg)// here use success not complete in complete it comes every time whether out put come or not 
    { 
    location:reload(); 
    } 
}); 
} 

參考ajax

+0

當我成功使用它永遠不會奏效? – Mystx

+0

在控制檯響應看到什麼是來看看這是你的網址文件和URL爲什麼你使用window.location的任何錯誤,我認爲這是造成問題,請在您的控制檯中看到的,我認爲這將有助於 –