2013-08-21 251 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的任何错误,我认为这是造成问题,请在您的控制台中看到的,我认为这将有助于 –