我想将id存储在全局变量中,然后存储此id以进行表单编辑 这是我的xml请求:
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));
return $response;
}
这是我的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 ) {
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 }) }}";
});
这是包含id:
的myjson响应 {"success":true,"title":"invoice","id":57,"notes":"gjgjgjgjg","accountid":1,"clientid":"5","status":"sent","totalamount":"90000","paidamount":"45000","balanceamount":"45000","createdby":1,"updatedby":1,"createddatetime":{"date":"2013-10-03 17:37:00","timezone_type":3,"timezone":"Asia\/Karachi"},"updateddatetime":{"date":"2013-10-03 17:37:00","timezone_type":3,"timezone":"Asia\/Karachi"}}
当我提醒invoiceid然后它显示result.id的值,但invoiceid没有传递给click功能,我怎么做?
答案 0 :(得分:1)
.done()
是回调。
您需要传递result.id或调用$('#edit')。完成回调中的click()
<script type="text/javascript">
var invoiceId;
$(document).ready(function(){
$("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;
//after setting you can call click like this
$('#edit').click(); // or use trigger()
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(){
= "{{ path('invoices_edit', {'id': invoiceid }) }}";
});
});
</script>