使用Javascript函数根据if语句选择某些Radio后,其他单选按钮不可单击

时间:2015-01-05 04:47:58

标签: javascript jquery html forms radio

我正在开发汽车服务预订系统,有三种不同的车辆选择。我正在尝试将其编码到如果列出了一定数量的乘客,车辆会自动切换到容纳更多人的东西并禁用第一辆车。

我已经完成了这个但是有问题。我使用数组和函数来定义乘客数量。然后我使用一个变量来添加这些函数吐出的值,以创建乘客总数。然后我设置了一个if语句来说明该值是否大于4,更改为此无线电输入并禁用第一个。

当发生这种情况时会出现问题我无法再选择第三种车辆选项而且我不知道原因。

我还尝试将乘客总值设置为如果小于5,则自动选择第一辆车。这也不起作用。

最终我希望它以这样一种方式行动:如果选择了超过14名乘客,则用户会收到错误(这很正常)。如果选择少于5名乘客,则选择第一辆车。如果选择超过4名乘客,则选择第二辆车,第三辆仍然是一个选项。如果选择超过8名乘客,则第一辆和第三辆车都被禁用。

希望我的代码格式不会过于严重。我是Javascript的新手,并且一直在拼凑教程。以下是表单部分jsfiddle无法正常工作。

以下是Javascript代码:

var adults_teenagers2 = new Array();
adults_teenagers2["1"] = 1;
adults_teenagers2["2"] = 2;
adults_teenagers2["3"] = 3;
adults_teenagers2["4"] = 4;
adults_teenagers2["5"] = 5;
adults_teenagers2["6"] = 6;
adults_teenagers2["7"] = 7;
adults_teenagers2["8"] = 8;
adults_teenagers2["9"] = 9;
adults_teenagers2["10"] = 10;
adults_teenagers2["11"] = 11;
adults_teenagers2["12"] = 12;
adults_teenagers2["13"] = 13;
adults_teenagers2["14"] = 14;

function AmountAdultsTeenagers() {
var AdultsTeenagersNumberAmount = 0;
var theForm = document.forms["resForm"];
var AdultsTeenagersNumber = theForm.elements["adults_teenagers"];
AdultsTeenagersNumberAmount = adults_teenagers2[AdultsTeenagersNumber.value];
return AdultsTeenagersNumberAmount;
}

var children2 = new Array();
children2["0"] = 0;
children2["1"] = 1;
children2["2"] = 2;
children2["3"] = 3;
children2["4"] = 4;
children2["5"] = 5;
children2["6"] = 6;
children2["7"] = 7;
children2["8"] = 8;

function Amountchildren() {
var childrenNumberAmount = 0;
var theForm = document.forms["resForm"];
var childrenNumber = theForm.elements["children"];
childrenNumberAmount = children2[childrenNumber.value];
return childrenNumberAmount;
}

var infants2 = new Array();
infants2["0"] = 0;
infants2["1"] = 1;
infants2["2"] = 2;
infants2["3"] = 3;
infants2["4"] = 4;
infants2["5"] = 5;
infants2["6"] = 6;
infants2["7"] = 7;
infants2["8"] = 8;

function Amountinfants() {
var infantsNumberAmount = 0;
var theForm = document.forms["resForm"];
var infantsNumber = theForm.elements["infants"];
infantsNumberAmount = infants2[infantsNumber.value];
return infantsNumberAmount;
}

function calculateTotal() {
var over = AmountAdultsTeenagers() + Amountchildren() + Amountinfants();
if(over>'14')
{
alert('Sorry, no vehicle can accomodate more than 14 passengers.');
document.getElementById("firstPageSubmit").disabled = true;

}
else {
document.getElementById("firstPageSubmit").disabled = false;
}
if(over<'5') {
document.getElementById("towncar").checked = true;
}
if(over>'4')
{
document.getElementById("towncar").disabled = true;
document.getElementById("stretch_limousine").disabled = false;
document.getElementById("passenger_van").checked = true;
}
else {document.getElementById("towncar").disabled = false;
}
}

和HTML:

<form method="post" class="res_form" id="resForm" name="res_form" onSubmit="return quoteCheck();">
<ul>
    <li id="steps">Step 1 of 3 - Select Type of Service/Get Quote</li>
    <li class="form_notice"><span>Reservations must be at least 48 hrs prior for towncars and 72 hrs for any other vehicle.</span></li>
    <li><fieldset class="res_form_fs" id="passengers">
    <legend>Passengers:</legend>
<label for="adults_teenagers" class="selectLabel">Adults/Teenagers:
        <select name="adults_teenagers" id="adults_teenagers" onchange="calculateTotal()">
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
            <option value="6">6</option>
            <option value="7">7</option>
            <option value="8">8</option>
            <option value="9">9</option>
            <option value="10">10</option>
            <option value="11">11</option>
            <option value="12">12</option>
            <option value="13">13</option>
            <option value="14">14</option>
        </select></label>
<label for="children" class="selectLabel">Children:
        <select name="children" id="children" onchange="calculateTotal()">
            <option value="0">None</option>
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
            <option value="6">6</option>
            <option value="7">7</option>
            <option value="8">8</option>
        </select></label>
<label for="infants" class="selectLabel">Infants:
        <select name="infants" id="infants" onchange="calculateTotal()">
            <option value="0">None</option>
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
            <option value="6">6</option>
            <option value="7">7</option>
            <option value="8">8</option>
        </select></label>
    </fieldset>
    </li>
    <li>
    <fieldset class="res_form_fs">
    <legend>Select Vehicle:</legend>
        <p class="radioT"><input type="radio" value="Towncar" onclick="calculateTotal()" name="vehicle" id="towncar" class="radio" unchecked /></p><p class="nonradioT"><span class="radioOption"><label for="towncar">Towncar</label></span></p><br />
        <p class="radioT"><input type="radio" value="Passenger Van" onclick="calculateTotal()" name="vehicle" id="passenger_van" class="radio" unchecked /></p><p class="nonradioT"><span class="radioOption"><label for="passenger_van">Passenger Van</label></span></p><br />
        <p class="radioT"><input type="radio" value="Stretch Limousine" onclick="calculateTotal()" name="vehicle" id="stretch_limousine" class="radio" unchecked /></p><p class="nonradioT"><span class="radioOption"><label for="stretch_limousine">Stretch Limousine</label></span></p>
    </fieldset>
    </li>
    <li><input name="submit" type="submit" id="firstPageSubmit" class="ressubmitButton" value="Continue to Make Reservation" /></li>
    </ul>
    </form>

1 个答案:

答案 0 :(得分:0)

在这里,我将所有选择选项的名称更改为相同,现在它可以正常工作。请参阅下面的JSFiddle:

<select name="adults_teenagers">

http://jsfiddle.net/ketan156/cthbg27h/8/

修改

如果条件改变:以下是新的JSFiddle:

http://jsfiddle.net/ketan156/cthbg27h/10/

还可以吗?根据我的理解,你喜欢得到它。

您需要我编辑的JSFiddle

http://jsfiddle.net/ketan156/cthbg27h/11/