gpt4 book ai didi

javascript - 如何从ajax获取表单ID并在symfony2的点击功能中设置它

转载 作者:行者123 更新时间:2023-11-30 05:43:53 25 4
gpt4 key购买 nike

我想将 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 没有传递给点击功能,我该怎么做?

最佳答案

.done() 是回调。

你需要传递result.id或者在done回调中调用$('#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>

关于javascript - 如何从ajax获取表单ID并在symfony2的点击功能中设置它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19156661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com