AJAX表单提交和刷新

时间:2012-12-18 18:05:22

标签: jquery ajax

我有一个表单,在更新时,需要从AJAX中提取新数据。我已经构建了一个在AJAX能够访问数据时输出成功的函数:

$(document).ready(function() {

    var results = $('#hidden').serialize();

    var url = 'index.php?option=com_mls&view=list&format=raw&' + results;

    $('#test').html(url);

    $.ajax({
        url: url,
        success: function(){
            alert('success');
        },
        error: function(){
            alert('There was an error loading your request.  <br />Please try again later.');
        }
    });
});

这已经过测试并且有效。我遇到的问题是在尝试提交用户表单时,设置一个包含新数据的AJAX函数。我试过这个无济于事:

$(document).ready(function() {

    function runQuery(){  

        var results = $('#hidden').serialize();

        var url = 'index.php?option=com_mls&view=list&format=raw&' + results;

        $('#test').html(url);

        $.ajax({
            url: url,
            success: function(){
                alert('success');
            },
            error: function(){
                alert('There was an error loading your request.  <br />Please try again later.');
            }
        });
    };  

    runQuery();

});

它没有运行runQuery();功能。

1 个答案:

答案 0 :(得分:2)

应该是

$(document).ready(function() {    
    runQuery();
});

function runQuery(){  

        var results = $('#hidden').serialize();

        var url = 'index.php?option=com_mls&view=list&format=raw&' + results;

        $('#test').html(url);

        $.ajax({
            url: url,
            success: function(){
                alert('success');
            },
            error: function(){
                alert('There was an error loading your request.  <br />Please try again later.');
            }
        });
    };