0

我创建了一个创建函数,我想将 id 存储在一个变量中,然后将此 id 传递给编辑函数,我为此编写了一些代码,但存在错误,我该怎么做?这是我的代码:

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

这是我的创建功能:

 if($request->isXmlHttpRequest()) {
        $response = new Response();
        $output = array('success' => true, 'title' => $entity->gettitle(), 'id' => $entity->getId(), 'notes' => $entity->getnotes(), 'accountid' => $entity->getaccountid(), 'clientid' => $entity->getClientID(), 'status' => $entity->getstatus(), 'totalamount' => $entity->getTotalAmount(), 'paidamount' => $entity->getPaidAmount(), 'balanceamount' => $entity->getBalanceAmount(), 'createdby' => $entity->getcreatedby(), 'updatedby' => $entity->getupdatedby(), 'createddatetime' => $entity->getcreateddatetime(), 'updateddatetime' => $entity->getupdateddatetime());
         $response->headers->set('Content-Type', 'application/json');
         $response->setContent(json_encode($output));

    }

这是我的编辑功能:

public function editAction($id)
{
    $em = $this->getDoctrine()->getManager();

    $entity = $em->getRepository('InvoicesInvoicesBundle:Invoices')->find($id);

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find Invoices entity.');
    }

    $editForm = $this->createForm(new InvoicesType(), $entity);
    $deleteForm = $this->createDeleteForm($id);

    return $this->render('InvoicesInvoicesBundle:Invoices:edit.html.twig', array(
        'entity'      => $entity,
        'edit_form'   => $editForm->createView(),
        'delete_form' => $deleteForm->createView(),
    ));
}

这是错误:

Variable "invoiceid" does not exist in InvoicesInvoicesBundle:Invoices:new.html.twig at line 362 
4

1 回答 1

2

如果您要在 JS 中动态生成路径,特别是如果您正在处理大量 AJAX 内容,那么您几乎肯定希望将 FOS 的JS Routing Bundle添加到您的项目中

正确配置后,路径的 JS 将类似于

Routing.generate('invoices_edit', { id: invoiceid });

可以在此处找到有关如何配置与 JSBundle 一起使用的路径和路由的说明

于 2013-10-07T11:59:44.067 回答