如何在点击功能中使用ajax结果?

时间:2013-10-03 09:14:29

标签: javascript jquery ajax symfony

我在symfony中创建一个表单,当我通过ajax提交表单然后我将(result.id)存储在一个变量中然后我在click函数中使用了这个变量但是出现错误在new.html.twig中不存在invoiceid 如何解决这个问题? 这是我的ajax代码:

$("form").submit(function(e) {              
    e.preventDefault();
    var url = $(this).attr('action');
    var data = $(this).serialize();
    $.ajax({
        type: "POST",
        url: url,
        data: data,
    }).done(function( result ) {
        var invoiceid=(result.id);
        if(result.success) {
            $('#result').css({
                'color':'black',
                'background-color':'#8F8',
                'display':'Block',
                'width':'200px'
            });
            $('#result').html('Invoices Record Inserted');
            setTimeout(function(){
                $('#result').hide();
            },3000);
        }
    });
    this.reset();
});
$("#edit").click(function(){         
    window.location.href= "{{ path('invoices_edit', {'id': invoiceid }) }}";  
});       

1 个答案:

答案 0 :(得分:0)

在JavaScript运行之前,您的模板化函数{{ path }}已得到解决。 您的模板引擎无法查看JavaScript值,模板中不存在变量invoiceid