dounao1875 2016-08-08 06:48
浏览 17
已采纳

如何在AJAX中显示变量?

I have a form where users enter 6 numbers, which are collect and stored as variables when the form is submitted. I'm then using AJAX to display a loading screen. I was wondering how to display the numbers entered on the loading screen using AJAX beforeSend function?

            $(function() {

            $(".submit").click(function() {

                var num1 = $("#num1").val();
                var num2 = $("#num2").val();
                var num3 = $("#num3").val();
                var num4 = $("#num4").val();
                var num5 = $("#num5").val();
                var num6 = $("#num6").val();
                var dataString = 'num1=' + num1 + '&num2=' + num2 + '&num3=' + num3 + '&num4=' + num4 + '&num5=' + num5 + '&num6=' + num6;
                if (num1 == '' || num2 == '') {
                    $('.error').fadeOut(200).show();
                } else {

                    $.ajax({
                        type : "POST",
                        url : "index.php",
                        data : dataString,

                        data : jQuery('#numbers_form').serializeArray(),
                        beforeSend : function() {
                            $('#loader').css('display', 'block');
                            $('#loader').css('margin', 'auto');

                        },
                        success : function(res) {
                            $('#success').css('display', 'block');
                        }
                    });
                }
            });
        });

展开全部

  • 写回答

1条回答 默认 最新

  • dtoka218420 2016-08-08 07:24
    关注

    Change

    else {
    
        $.ajax({
    

    to

    else {
        $('body').append(num1+' '+num2+' '+num3+' '+num4+' '+num5+' '+num6);
        $.ajax({
    

    That should do it. If it is absolutely mandatory to use beforeSend then make it

    $.ajax({
        type : "POST",
        url : "index.php",
        data : dataString,
        beforeSend : function() {
        $('#loader').css('display', 'block');
        $('#loader').css('margin', 'auto');
        $('body').append(num1+' '+num2+' '+num3+' '+num4+' '+num5+' '+num6);
        },
        success : function(res) {
                $('#success').css('display', 'block');
        }
    });
    

    You can change the 'body' selector to any other element and append function to text() or html() as well.

    Note: You have assigned data twice. That would be unnecessary if the inputs are only 6 numbers.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部