0

我必须传递一个不在我的表单中的变量,所以我在提交之前使用了 ajax 请求

$("#bouton").click(function(){
        dureechoisi = $("#duree1").text().replace(/\D/g,'');
        alert(dureechoisi);
            $.ajax({ 
            type: "POST",
            url: "/iframe/simu-test.php",
            data : {
                dureechoisi1 : dureechoisi
            },
            dataType: 'json',
            xhrFields: {
            withCredentials: true
            },
            async: true
        });
         $("#resultatform").submit(); 
    });

得到我的变量:$dureechoisi = $_POST['dureechoisi1'];

我的帖子变量为空,我做错了什么?

4

1 回答 1

0

如果在提交之前使用 ajax,则使用 ajax 的回调,因为 ajax 是异步函数。

你可以这样做。

$("#bouton").click(function(){
    dureechoisi = $("#duree1").text().replace(/\D/g,'');
    alert(dureechoisi);
        $.ajax({ 
        type: "POST",
        url: "/iframe/simu-test.php",
        data : {
            dureechoisi1 : dureechoisi
        },
        dataType: 'json',
        xhrFields: {
        withCredentials: true
        },
        async: true,
        success: function() {
            $("#resultatform").submit(); 
        }
    });
});

阅读:https ://www.w3schools.com/jquery/ajax_ajax.asp

于 2018-09-12T15:47:59.423 回答