我正在尝试根据用户输入的总人数计算房间总数。最多人数不能超过7人。房间类型为双床间(每间客房2人 - 成人和儿童),双人间(每间客房2人 - 成人和儿童),单人间(每间客房1人)和加床。人的类型是成人,带床的孩子和没有床的孩子。
对于房间计算,我们需要添加[(成人+带床的孩子/ 2)]以获得房间价值。默认情况下,系统必须将房间数放在双人共享中。带床的儿童必须与成人一起入住双床间。所以,任何超过奇数的都将导致单人间。
示例:4名成人和1名儿童;共5人。 结果:2间双床房和1间单人房。
用户可以通过将双人床改为双人床或加床来改变这一结果。 结果:2张双人床和1张加床。加床与双人间相连。
我需要一些帮助才能在JavaScript中使用此功能。到目前为止,我设法进行了系统计算,但是当尝试通过手动进行更改时,它会导致计算错误。
示例:4名成人和1名儿童;共5人。 默认值:2间双床房和1间单人房。 用户更改为1个双胞胎(必须至少有1个双胞胎,因为有一个孩子),单人可以最多2个房间,余额1个成人可以选择1个加床。
我需要一些帮助:
function cal_rooms()
{
var have_extra_bed="<?=$room_have_extra_bed;?>";
var adult = document.getElementById("adult").value;
var childbed = document.getElementById("childbed").value;
var childwbed = document.getElementById("childwbed").value;
document.getElementById("doubleroom").value="0";
//var totals1=(parseInt(adult));
var totals=(parseInt(adult)+parseInt(childbed)+parseInt(childwbed));
if(adult==0)
{
document.getElementById("childbed").disabled=true;
document.getElementById("childwbed").disabled=true;
document.getElementById("childbed").value="0";
document.getElementById("childwbed").value="0";
document.getElementById("totalperson").value="";
}
else
{
document.getElementById("childbed").disabled=false;
document.getElementById("childwbed").disabled=false;
if(totals>7)
{
var newtots=(totals-parseInt(adult));
alert("Cannot exceed 7!");
document.getElementById("adult").value=0;
document.getElementById("childbed").value="0";
document.getElementById("childwbed").value="0";
document.getElementById("totalperson").value="";
}
else
{
document.getElementById("totalperson").value=totals;
///rooms
var numrooms=((parseInt(adult)+parseInt(childbed))/2);
var new_numrooms=Math.ceil(numrooms);
var check_numrooms=(parseInt(new_numrooms)-numrooms);
var single_num=Math.ceil(check_numrooms);
var twin_num=(parseInt(new_numrooms)-parseInt(single_num));
var doubleroom=document.getElementById("doubleroom").value;
var totals_room1=(parseInt(twin_num)+parseInt(doubleroom)+parseInt(single_num));
if(have_extra_bed=="Y")
{
if(parseInt(adult)>=parseInt(childbed))
{
var max_single_can_select=(parseInt(adult)-parseInt(childbed));
document.getElementById("twin").value=twin_num;
document.getElementById("single").value=single_num;
document.getElementById("temp_single").value=single_num;
document.getElementById("def_single").value=single_num;
document.getElementById("max_single").value=max_single_can_select;
document.getElementById("extrabed").value="0";
document.getElementById("totalroom").value=totals_room1;
document.getElementById("totalextra").value="0";
document.getElementById("single").disabled=false;
}
else
{
document.getElementById("twin").value=twin_num;
document.getElementById("extrabed").value=single_num;
document.getElementById("single").value="0";
document.getElementById("temp_single").value="0";
document.getElementById("def_single").value="0";
document.getElementById("max_single").value="0";
var extra_a=document.getElementById("extrabed").value;
var single_a=document.getElementById("single").value;
var twin_a=document.getElementById("twin").value
var doubleroom=document.getElementById("doubleroom").value;
var totals_room1=(parseInt(single_a)+parseInt(twin_a)+parseInt(doubleroom));
document.getElementById("totalroom").value=totals_room1;
document.getElementById("totalextra").value=extra_a;
document.getElementById("single").disabled=true;
}
}
else
{
if(parseInt(adult)>=parseInt(childbed))
{
var max_single_can_select=(parseInt(adult)-parseInt(childbed));
document.getElementById("twin").value=twin_num;
document.getElementById("single").value=single_num;
document.getElementById("temp_single").value=single_num;
document.getElementById("def_single").value=single_num;
document.getElementById("max_single").value=max_single_can_select;
document.getElementById("extrabed").value="0";
document.getElementById("totalroom").value=totals_room1;
document.getElementById("totalextra").value="0";
document.getElementById("single").disabled=false;
document.getElementById("extrabed").disabled=true;
}
else
{
document.getElementById("twin").value=twin_num;
document.getElementById("single").value=single_num;
document.getElementById("temp_single").value=single_num;
document.getElementById("def_single").value=single_num;
document.getElementById("max_single").value="0";
document.getElementById("extrabed").value="0";
document.getElementById("single").disabled=true;
document.getElementById("extrabed").disabled=true;
var extra_a=document.getElementById("extrabed").value;
var single_a=document.getElementById("single").value;
var twin_a=document.getElementById("twin").value
var doubleroom=document.getElementById("doubleroom").value;
var totals_room1=(parseInt(single_a)+parseInt(twin_a)+parseInt(doubleroom));
document.getElementById("totalroom").value=totals_room1;
document.getElementById("totalextra").value=extra_a;
}
}
return surcharge();
}
}
}
function cal_single()
{
///Start single function
var choose_single = document.getElementById("single").value;
var single_pick = document.getElementById("single_pick").value;
var extra_pick = document.getElementById("extra_pick").value;
var checked_for_doubleroom = document.getElementById("temp_double").value;
var checked_for_single = document.getElementById("temp_single").value;
var checked_for_def_single = document.getElementById("def_single").value;
var total_pax_adult = document.getElementById("adult").value;
var total_pax_childbed = document.getElementById("childbed").value;
var total_pax_bed = (parseInt(total_pax_adult)+parseInt(total_pax_childbed));
if (parseInt(total_pax_bed)%2 == 0)
{
////Even Number
//document.getElementById("temp_single").value=choose_single;
var temp_twin = document.getElementById("twin").value;
var temp_double = document.getElementById("temp_double").value;
var temp_single = document.getElementById("temp_single").value;
var temp_extra = document.getElementById("temp_extra").value;
var total_temp = ((parseInt(temp_twin)*2)+(parseInt(temp_double)*2)+parseInt(temp_single)+parseInt(temp_extra));
var new_total_temp = ((parseInt(temp_twin)*2)+(parseInt(temp_double)*2)+parseInt(choose_single)+parseInt(temp_extra));
if(total_temp>total_pax_bed)
{
alert("Total Number of Room Exceed Total number of Pax!");
document.getElementById("single").value=temp_single;
return false;
}
else
{
var def_single=document.getElementById("def_single").value;
var doubleroom=document.getElementById("doubleroom").value;
var single=document.getElementById("single").value;
var adult = document.getElementById("adult").value;
var childbed = document.getElementById("childbed").value;
///rooms
var numrooms=((parseInt(adult)+parseInt(childbed))/2);
var new_numrooms=Math.ceil(numrooms);
var check_numrooms=(parseInt(new_numrooms)-numrooms);
var single_num=Math.ceil(check_numrooms);
var twin_num=(parseInt(new_numrooms)-parseInt(single_num));
var max_sing=document.getElementById("max_single").value;
if(single>max_sing)
{
if(max_sing==0)
{
alert("Single cannot been select");
document.getElementById("single").value=def_single;
return false;
}
else
{
alert("Single cannot exceed "+max_sing);
document.getElementById("single").value=def_single;
return false;
}
}
else
{
if(single==0)
{
return false;
}
else
{
var latest_twin_num=(parseInt(twin_num)-parseInt(single));
var min_twin_sh=childbed;
if(parseInt(latest_twin_num)<=parseInt(min_twin_sh))
{
var new_latest_twin=min_twin_sh;
}
else
{
var new_latest_twin=latest_twin_num;
}
document.getElementById("single").value=single;
document.getElementById("twin").value=new_latest_twin;
document.getElementById("max_single").value=new_latest_twin;
document.getElementById("temp_single").value=single;
document.getElementById("single_pick").value="Y";
var extra_a=document.getElementById("extrabed").value;
var single_a=document.getElementById("single").value;
var twin_a=document.getElementById("twin").value
var doubleroom=document.getElementById("doubleroom").value;
var totals_room1=(parseInt(single_a)+parseInt(twin_a)+parseInt(doubleroom));
document.getElementById("totalroom").value=totals_room1;
document.getElementById("totalextra").value=extra_a;
}
}
document.getElementById("single").disabled=true;
}
}
else
{
答案 0 :(得分:0)
尝试使用javascript函数模x= y%2
这将为您提供所需的确切房间,如果有提醒,您可以再添加一个房间。