Ajax数据无法识别已定义的变量

时间:2019-04-06 10:53:21

标签: jquery ajax

我早些时候问了一个有关此问题(已删除)的问题,我将以下内容作为三个单独的脚本。我现在走的很远,并结合了这些功能,但出现了错误。

基本上,我基于id获得Month的单选按钮的值,以及Year的另一个单选按钮的值。我将它们设置为jquery中的变量,并按预期方式在控制台中显示。它们还在div中显示,我正在使用id $("#Year_Display").empty();引用$("#Year_Display").append(year);。正如您在下面看到的,我在Month上的工作也是如此。

我想使用ajax发布这些变量,所以我可以用相应的月份和年份填充<div>

这里是我所拥有的:

<script>
//Selects Month and Year Value
    $(document).ready(function(){

    $("#Jan, #Feb, #Mar, #Apr, #May, #Jun, #Jul, #Aug, #Sep, #Oct, #Nov, #Dec") // select the radio by its id
    .change(function(){
        if( $(this).is(":checked") ){ // check if the radio is checked
            var month = $(this).val(); 
            console.log(month); 
            $("#Month_Display").empty();
            $("#Month_Display").append(month);

            }
        });

    $("#1, #2, #3, #4") 
    .change(function(){ 
        if( $(this).is(":checked") ){ 
            var year = $(this).val(); 
            console.log(year); 
            $("#Year_Display").empty();
            $("#Year_Display").append(year);

            }
        });

        // ajax retrieve
        $.ajax({

            url:'includes/handlers/ajax_load_data.php',
            type:'POST',
            data:{'month':month, 'year':year},

                success: function(data) {
                    $('#myDIV').html(data);

                        }

                });
    });
</script>

我遇到的错误是jquery-3.3.1.min.js:2 Uncaught ReferenceError: month is not defined,但是当我单击不同的月份和年份时,它们在控制台和页面上均按预期显示。

我从事这项工作已经很久了,不求助;我一直在这里和其他站点的答案中将其拼凑在一起。如您所见,我的jquery / ajax并不是那么好,所以任何帮助/引用都将不胜感激。

1 个答案:

答案 0 :(得分:1)

我在代码中添加了一些更改,可以在全局级别声明var,并且可以在所有函数中使用它,因此使用此方法不会给未定义月份的错误, 希望对您有帮助!

//选择月份和年份值

$(document).ready(function () {
    var month;
    var year;
    $("#Jan, #Feb, #Mar, #Apr, #May, #Jun, #Jul, #Aug, #Sep, #Oct, #Nov, #Dec") // select the radio by its id
        .change(function () {
            if ($(this).is(":checked")) { // check if the radio is checked
                month = $(this).val();
                console.log(month);
                $("#Month_Display").empty();
                $("#Month_Display").append(month);
                if (month != null && month != '' && year != null && year != '') {
                    loadData(month, year);
                }
            }
        });

    $("#1, #2, #3, #4")
        .change(function () {
            if ($(this).is(":checked")) {
                year = $(this).val();
                console.log(year);
                $("#Year_Display").empty();
                $("#Year_Display").append(year);
                if (month != null && month != '' && year != null && year != '') {
                    loadData(month, year);
                }
            }
        });

    // ajax retrieve
    function loadData(month, year) {
        $.ajax({

            url: 'includes/handlers/ajax_load_data.php',
            type: 'POST',
            data: { 'month': month, 'year': year },

            success: function (data) {
                $('#myDIV').html(data);

            }

        });
    };

});
相关问题