0

我有这个 ajax 设置,当用户更改下拉列表的选择时,它会执行一些 php 计算,然后它会发回结果并输出它们。这一切都很好,除了当用户第一次加载他们受到欢迎的屏幕时,只有空的结果框,并且只有当他们在其中一个下拉框中更改选择时,才会出现结果。我的问题是,无论如何在加载窗口时运行 ajax,所以可以输出默认的下拉选择。

这是ajax代码,如果您需要其他内容,请告诉我:

  <script>
$("document").ready(function (){ 

    $(".add_detail_dropdown").change(function(){


        var m = document.getElementById('meter_square');
        var meter_square = m.options[m.selectedIndex].value;

        var s = document.getElementById('story_height');
        var story_height = s.options[s.selectedIndex].value;

        var r = document.getElementById('roof_type');
        var roof_type = r.options[r.selectedIndex].value;

        var q = document.getElementById('material_quality');
        var material_quality = q.options[q.selectedIndex].value;

        var w = document.getElementById('wall_type');
        var wall_type = w.options[w.selectedIndex].value;

        var f = document.getElementById('flooring');
        var flooring = f.options[f.selectedIndex].value;

     $.ajax({
            type: "GET",
            url: "add_extension_calc.php",
            data: { meter_square: meter_square, story_height: story_height, roof_type: roof_type, material_quality: material_quality, wall_type: wall_type, flooring: flooring, estimated_wealth: <?php print "$estimated_wealth";?>, gain_percent: <?php print "$addon_gain_percent";?>  },
            dataType: "json",
            statusCode: {
                200: function (response) {
                                            $("#res_expected_gain").html(response.total_gain);
                                            $("#res_expected_house_price").html(response.house_price);
                                            $("#res_total_supply_cost").html(response.store_price);
                                            $("#res_total_supply_time").html(response.store_time);
                                            $("#res_expected_profit").html(response.expected_profit);
                                         }

            }
        });
})
});
</script>
4

1 回答 1

2

简单地使用change(),它会触发你的改变..

<script>
    $("document").ready(function (){ 

        $(".add_detail_dropdown").change(function(){
            // ............
        });

        $(".add_detail_dropdown").change();
    });
</script>
于 2012-08-26T00:06:36.243 回答