2010-08-13 108 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 
    } 
    }); 
});