2010-08-13 106 views
3

我使用codeigniter框架,並在視圖中我使用jQuery。現在,我有一個包含數據庫值的選擇框。我使用jquery change event和alert box來測試它的值。它提供了更改警報框中的確切值,但是當我使用post方法或get或甚至$ .ajax()時,它不會給出任何輸出。事實上,我用console.log()進行了檢查,它也不在裏面。我只需要發佈一些值並從數據庫中獲取一些信息以顯示在該選擇框下方的div中。 下面的代碼的jQuery:jquery的奇怪行爲

$(document).ready(function(){ 
    $('#org_name').change(function(){ 
     $.post('index.php/contact/getorg',{'query':$('#org_name').val()},function(data){ 
     console.log("inside post"); 
     $('#showorg').html(data); 
     console.log(org_name); 
     });//close post function 
    }); //close org_name event function 
}); 
+0

您可以發佈您的SELECT框的HTML代碼嗎?除非你的表單提交附加了一個事件,否則jQuery不應該影響發佈到服務器的數據。 – 2010-08-13 12:45:55

+1

該頁面是否在服務器上被擊中?在「數據」中返回的內容是html? – 2010-08-13 12:48:12

+0

@Ryan - 這不是'

'提交這是發生在這裏,他通過'$ .post()'進行AJAX調用,即使有一個表單提交處理程序,它不應該對他的'該職位的成功處理程序。 – 2010-08-13 12:48:35

回答

1

我總是用下面的風格...

ajax({ 
    type: "POST", 
    url: 'index.php/contact/getorg', 
    data: JSON.stringify({'query':$('#org_name').val()}), 
    dataType: "json", 
    contentType: "application/json; charset=utf-8", 
    success: function (data) { 
     $('#showorg').html(JSON.parse(data.d)); 
    }, 
    error: showError 
}; 

function showError(responseText, statusText, xhr, $form) { 
      debugger;   } 

編輯:

而且行console.log(org_name);似乎不正確。成員org_name從哪裏來?

+0

如果這是問題,不知道。也許你的網址是相對的?試試'/index.php/contact/getorg'。另外,用firefox與firefox來診斷xhr(ajax請求)。然後你可以看到真正發生了什麼。 – Matthew 2010-08-13 13:33:43

1

嘗試使用.ajax jQuery的方法與方法參數指定的失敗功能(error變量)。如果出亂子在服務器端,或者您有其他特定的錯誤,你就可以分析XMLHttpRequest參數url errorThrown變量

0

感謝回覆的傢伙,錯誤回調方法幫了我很多。這是給404方法,所以我改變了網址。現在它的工作就像魅力。讓我與大家分享我所做的一切:

$('#org_name').bind('change',function(){ 
    $("#showorg").html("wait..."); 
    $.ajax({ 
    url: 'http://localhost/ifes/index.php/contact/getorg', 
    type: 'POST', 
    dataType: 'html', 
    data:{'query':$('#org_name').val()}, 
    timeout: 1000, 
    error: function(xhr,err){ 
    alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status); 
    alert("responseText: "+xhr.responseText); 
    }, 
    success: function(data){ 
     $('#showorg').html(data);// do something with data 
    } 
    }); 
});