在带有ajax调用的模态上加载php片段并从表单中发布,导致多次调用它(Loading a php snippet on a modal with an ajax call and posting f

编程入门 行业动态 更新时间:2024-10-25 01:24:26
在带有ajax调用的模态上加载php片段并从表单中发布,导致多次调用它(Loading a php snippet on a modal with an ajax call and posting from the form causing to call it multiple times)

在一个形式我有一个模态骨架 - 意味着模态体是空的。 我设计了一些动态添加(用于下拉列表)表单并加载ajax后调用

var scriptname,initiator; $(".launch-add-form").click(function(e){ scriptname=($(this).prop('id')).split("-")[0] ; initiator=$(this).prop('id'); $.ajax({ type:"post", url:"<?=site_url()?>/admin/commoncontroller/getform/", data:{csrftoken:cct,scriptname:scriptname} }).done(function(res){ $("#modalBody").html(res) ;}) });

返回与脚本名称匹配的任何脚本,并将响应呈现给模态体。 那么现在从模态我填写表格并保存它正常工作,但问题是在主窗体上有多个下拉菜单,每个都有

.launch-add-form class

好吧,如果我用一个下拉保存并尝试使用其他,那么就像几何级数一样调用ajax保存调用...第一个1个呼叫比2个呼叫然后4个呼叫...

有人可以帮忙吗?

In a form I have a modal skeleton--meaning the modal body is empty. I designed number of on the fly add(for dropdowns) form and loading with a ajax post call like

var scriptname,initiator; $(".launch-add-form").click(function(e){ scriptname=($(this).prop('id')).split("-")[0] ; initiator=$(this).prop('id'); $.ajax({ type:"post", url:"<?=site_url()?>/admin/commoncontroller/getform/", data:{csrftoken:cct,scriptname:scriptname} }).done(function(res){ $("#modalBody").html(res) ;}) });

Which returns any script that matches the script name and render the response to the modal body. Well now from modal i fill the form and save it which properly works but the problem is on the main form there are multiple drop-downs and each have

.launch-add-form class

Well, if I save with one drop down and try to do with other, then the ajax save call is being called like in geometric progression...1st 1 call than 2 calls then 4 calls...

Can anyone help please?

最满意答案

我猜你是多次分配onClick事件。

在添加新事件之前尝试解除任何clic事件的绑定:

$(".launch-add-form").unbind( "click" );

所以你的代码将是:

var scriptname,initiator; $(".launch-add-form").unbind( "click" ); $(".launch-add-form").click(function(e){ scriptname=($(this).prop('id')).split("-")[0] ; initiator=$(this).prop('id'); $.ajax({ type:"post", url:"<?=site_url()?>/admin/commoncontroller/getform/", data:{csrftoken:cct,scriptname:scriptname} }).done(function(res){ $("#modalBody").html(res) ;}) });

I guess you are assigning the onClick event multiple times.

try unbinding any clic event before adding a new one:

$(".launch-add-form").unbind( "click" );

so your code will be:

var scriptname,initiator; $(".launch-add-form").unbind( "click" ); $(".launch-add-form").click(function(e){ scriptname=($(this).prop('id')).split("-")[0] ; initiator=$(this).prop('id'); $.ajax({ type:"post", url:"<?=site_url()?>/admin/commoncontroller/getform/", data:{csrftoken:cct,scriptname:scriptname} }).done(function(res){ $("#modalBody").html(res) ;}) });

更多推荐

本文发布于:2023-08-03 08:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1385252.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   并从   片段   加载   模态

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!