我有一个像这样的 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/