dtpw54085 2013-10-25 23:00
浏览 54

在form_validation失败后,带有AJAX下拉菜单的Codeigniter无法正常工作并重定向/重新加载视图

Once the validation runs, fails, and proceeds to redirect to the same controller index.

    if (!$this->form_validation->run())
    {
        $this->index();
    }

The form is a simple, two dropdowns, one dependent on the other, once the view is reloaded though, the second dependent dropdown stops working.

Here is the JS

    $(document).ready(function()
    {
$('#manufacturer_dropdown').change(function()
{

    $("#sw_names > option").remove();
    var id = $('#manufacturer_dropdown').val();
    $.ajax({
        type: "POST",
        url: "software/get_software_names/"+id,
        datatype : "JSON",

        success: function(sw_names)
        {
            $.each(sw_names,function(id,software_name)
            {
                var opt = $('<option />');
                opt.val(software_name);
                opt.text(software_name);
                $('#sw_names').append(opt);
            });
        }
    });
});
    });

If I manually refresh the page, the dropdowns work again. Not sure what is happening, maybe the document ready function stops checking or something. I'm no JS expert...

I can post more of the code if needed.

Edit: My JS URL was missing a /

Works now

  • 写回答

1条回答 默认 最新

  • duannaozhao4626 2013-10-25 23:05
    关注

    If you are making an ajax request, you must give an ajax response

    You cannot redirect the page in the middle of a request like that.

    I've written something up going through all of this type of problem, as it's such a common one. Hopefully this will steer you along: http://codebyjeff.com/blog/2013/04/how-do-i-use-ajax-with-framework-x

    评论

报告相同问题?