javascript - 在 ajax 调用之前显示模态形式并从中获取数据

标签 javascript jquery ajax

我有一个像这样的 ajax 函数:

 function foo(e, e1, curc)
 {
    var sender = (e && e.target) || (window.event && window.event.srcElement);

    $.ajax({
        type: 'POST',
        url: 'script.php',
        dataType: 'json',
        data: "id="+e+"&mod="+e1+"&curc="+curc, 
        beforeSend: function() {
            $('#mform').show();             
        },
        complete: function() {
            $('#fountainG').hide();
        }, 
        success: function(data) {
            document.getElementById("itog").innerHTML = data.d+data.a;
        },
        error: function(xhr) {
            document.getElementById("itog").innerHTML = '123';
        }    
    });
}

我需要向用户显示一些模态表单,并在 ajax 脚本中从中获取数据。我试图在发送前向 ajax 添加显示功能 - 但我不明白如何等待用户表单提交,以及如何从模态表单获取数据。 html 中的 Ajax 函数调用:href="javascript:void(0)"onclick="javascript:foo(3800064420557,1,138)

最佳答案

你只需要重新安排你的逻辑。与其尝试在 ajax 请求“内部”显示模态,不如推迟发送 ajax 请求,直到您从模态中获得必要的数据。这是一个粗略的大纲,假设您的模态元素 $('#mform') 中有一个 id 为 myform 的表单,这是您要从中获取数据的表单。

function foo(e, e1, curc)
 {
    var sender = (e && e.target) || (window.event && window.event.srcElement);

    var modal = $('#mform');
    var form = $('#myform', modal);
    form.on( 'submit', function(){
        $('mform').hide();

        // make your ajax call here the same way, and inside the
        // onsuccess for this ajax call you will then have access to both
        // the results of your ajax call and the results of the form
        // data from your modal.
        $.ajax({ ... });
    });
}

关于javascript - 在 ajax 调用之前显示模态形式并从中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44112375/

相关文章:

javascript - 在 AngularJS 中使用延迟结果

javascript - 包含的 HTML 模板中的 AngularJS id 属性

php - 从php中的Ajax调用获取2个变量

javascript - 使用 Django 构建 Ajax 自动分页器

javascript - 传单地理搜索 : return lat/lng from address found event

javascript - 这种声明的目的是什么?

javascript - 计算 AngularJS 中特定检查值出现的次数

javascript - 在相关容器中动态减小字体大小

javascript - 在knockout.js中创建observableArray的动态数组

javascript - XMLHttpRequest onprogress total 在 Chrome 上始终为 0