Php 首次加载窗口时运行onchange ajax
我有一个ajax设置,当用户更改下拉列表的选择时,它会执行一些php计算,然后返回结果并输出它们。这一切都可以正常工作,除了当用户第一次加载屏幕时,他们只收到空的结果框,并且只有当他们更改其中一个下拉框中的选择时,结果才会出现。我的问题是,在加载窗口时是否有运行ajax的方法,这样就可以输出默认的下拉选择 这是ajax代码,如果您还需要什么,请告诉我:Php 首次加载窗口时运行onchange ajax,php,mysql,ajax,json,Php,Mysql,Ajax,Json,我有一个ajax设置,当用户更改下拉列表的选择时,它会执行一些php计算,然后返回结果并输出它们。这一切都可以正常工作,除了当用户第一次加载屏幕时,他们只收到空的结果框,并且只有当他们更改其中一个下拉框中的选择时,结果才会出现。我的问题是,在加载窗口时是否有运行ajax的方法,这样就可以输出默认的下拉选择 这是ajax代码,如果您还需要什么,请告诉我: <script> $("document").ready(function (){ $(".add_detail_d
<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>
$(“文档”).ready(函数(){
$(“.add\u detail\u dropdown”).change(函数(){
var m=document.getElementById('meter_square');
var meter_square=m.options[m.selectedIndex]。值;
var s=document.getElementById('story_height');
var story_height=s.options[s.selectedIndex].值;
var r=document.getElementById('roof_type');
var roof_type=r.options[r.selectedIndex]。值;
var q=document.getElementById(“材料质量”);
var material_quality=q.options[q.selectedIndex]。值;
var w=document.getElementById('wall_type');
var wall_type=w.options[w.selectedIndex]。值;
var f=document.getElementById(“地板”);
var flooding=f.options[f.selectedIndex]。值;
$.ajax({
键入:“获取”,
url:“add_extension_calc.php”,
数据:{平方米:平方米,层高:层高,屋顶类型:屋顶类型,材料质量:材料质量,墙壁类型:墙壁类型,地板:地板,估计财富:,收益百分比:},
数据类型:“json”,
状态代码:{
200:功能(响应){
$(“#res#u expected_gain”).html(response.total_gain);
$(“#res#u expected_house_price”).html(response.house_price);
$(“#res#u total_supply_cost”).html(response.store_price);
$(“#res#u total_supply_time”).html(response.store_time);
$(“#res#u expected_profice”).html(response.expected_profice);
}
}
});
})
});
只需使用change()
,它就会触发您的更改
<script>
$("document").ready(function (){
$(".add_detail_dropdown").change(function(){
// ............
});
$(".add_detail_dropdown").change();
});
</script>
$(“文档”).ready(函数(){
$(“.add\u detail\u dropdown”).change(函数(){
// ............
});
$(“.add_detail_dropdown”).change();
});
好的,太棒了,真不敢相信我以前没有想到这一点。谢谢你的帮助