首次加载窗口时运行onchange ajax

时间:2012-08-26 00:02:12

标签: php mysql ajax json

我有这个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>

1 个答案:

答案 0 :(得分:2)

只需使用change(),就会触发您的更改..

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

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

        $(".add_detail_dropdown").change();
    });
</script>