我无法使用if语句(javascript)从html选择菜单打印值
我已经有一段时间遇到这些问题了,我不知道如何使可变座位等于“阳台”、“较低区域”、“一级”或“二级” 我已尝试使用.select,制作了一些失败的数组,.checked 我需要能够使seatch变量等于一个值,这取决于他们选择的选择菜单的哪个位,我使用的是if语句 我将在下面添加我的html和Javascript我无法使用if语句(javascript)从html选择菜单打印值,javascript,html,user-input,Javascript,Html,User Input,我已经有一段时间遇到这些问题了,我不知道如何使可变座位等于“阳台”、“较低区域”、“一级”或“二级” 我已尝试使用.select,制作了一些失败的数组,.checked 我需要能够使seatch变量等于一个值,这取决于他们选择的选择菜单的哪个位,我使用的是if语句 我将在下面添加我的html和Javascript <div data-role="fieldcontain"> <label for="selectmenu" class="select">Pre
<div data-role="fieldcontain">
<label for="selectmenu" class="select">Preferred Seating:</label> <!-- Following drop down checkbox -->
<select name="selectmenu" id="selectmenu">
<option name="selectmenu" value="200" class="lowerLvl" id="lowerArea" >Lower Area($200)</option>
<option name="selectmenu" value="150" class="levelOne" selected="selected" id="levelOne">Level 1($150)</option>
<option name="selectmenu" value="100" class="levelTwo" id="levelTwo">Level 2($100)</option>
<option name="selectmenu" value="200" class="balcony" id="balcony">Balcony($200)</option>
</select>
</div>
<!--End of DropDownBoxes-->
<!--START OF CHECK BOXES-->
<div data-role="fieldcontain">
<fieldset data-role="controlgroup">
<legend>Prefered night:</legend>
<input type="radio" name="checkbox1" id="checkbox1_0" class="custom" value="" checked="checked" /></option>
<label for="checkbox1_0">Thursday</label>
<br />
<input type="radio" name="checkbox1" id="checkbox1_1" class="custom" value="" />
<label for="checkbox1_1">Friday</label>
<br /><!--Break as on Example-->
<input type="radio" name="checkbox1" id="checkbox1_2" class="custom" value="" />
<label for="checkbox1_2">Saturday</label>
</fieldset><!-- Above are check boxes -->
</div>
<div data-role="fieldcontain">
<fieldset data-role="controlgroup" data-type="horizontal">
<legend>Subscribe to weekly newsletter: </legend>
<input type="checkbox" name="newletter" id="news" class="custom" value="" />
</fieldset>
</div>
<!--END OF CHECK BOXES-->
<!--Put a tick box here that asks for weekly mail-->
<button type="submit" value="Register" onClick="validateGalaOrder()"></button>
<p id="OrderInput"></p><!--USERS INPUT RETURNS TO THIS <P>-->
<p id="tktCost"></p>
<p id="orderErrorMsg"></p><!--INCORRECT INPUT MESSAGES RETURN TO THIS <P>-->
下面是我的JavaScript
请注意,一些JavaScript来自选择菜单上方html的其他部分
目前,我正在从select菜单中获取“value”,它位于可变成本下的else语句中
我只是想快速提醒一下我要做什么:
我需要在OrderInput变量中打印一个seatch变量值
此座位变量需要是“较低区域”、“一级”“阳台”或“二级”,具体取决于他们在选项上单击的内容编辑2:seatch=document.getElementById'selectmenu.options[document.getElementById'selectmenu.selectedIndex].text.split[0]
感谢蒂姆抓住了这个机会
编辑:查看代码后,我想您可以将变量声明为seatch=document.getElementById'selectmenu.options[document.getElementById'selectmenu.selectedIndex].text
获取元素的.selectedIndex并使用它来获取元素的.options[selectedIndex].text,这是您需要的文本
这是一个。因此@flowstoneknight给出了正确的答案。我的新代码如下。我现在可以得到这个值并将其乘以输入的票数,还可以在orderInput中打印座位的名称 我的htmlIt没有改变'
<div data-role="fieldcontain">
<label for="selectmenu" class="select">Preferred Seating:</label> <!-- Following drop down checkbox -->
<select name="selectmenu" id="selectmenu">
<option name="selectmenu" value="200" class="lowerLvl" id="lowerArea" >Lower Area($200)</option>
<option name="selectmenu" value="150" class="levelOne" selected="selected" id="levelOne">Level 1($150)</option>
<option name="selectmenu" value="100" class="levelTwo" id="levelTwo">Level 2($100)</option>
<option name="selectmenu" value="200" class="balcony" id="balcony">Balcony($200)</option>
</select>
</div>
<!--End of DropDownBoxes-->
<!--START OF CHECK BOXES-->
<div data-role="fieldcontain">
<fieldset data-role="controlgroup">
<legend>Prefered night:</legend>
<input type="radio" name="checkbox1" id="checkbox1_0" class="custom" value="" checked="checked" /></option>
<label for="checkbox1_0">Thursday</label>
<br />
<input type="radio" name="checkbox1" id="checkbox1_1" class="custom" value="" />
<label for="checkbox1_1">Friday</label>
<br /><!--Break as on Example-->
<input type="radio" name="checkbox1" id="checkbox1_2" class="custom" value="" />
<label for="checkbox1_2">Saturday</label>
</fieldset><!-- Above are check boxes -->
</div>
<div data-role="fieldcontain">
<fieldset data-role="controlgroup" data-type="horizontal">
<legend>Subscribe to weekly newsletter: </legend>
<input type="checkbox" name="newletter" id="news" class="custom" value="" />
</fieldset>
</div>
<!--END OF CHECK BOXES-->
<!--Put a tick box here that asks for weekly mail-->
<button type="submit" value="Register" onClick="validateGalaOrder()"></button>
<p id="OrderInput"></p><!--USERS INPUT RETURNS TO THIS <P>-->
<p id="tktCost"></p>
<p id="orderErrorMsg"></p><!--INCORRECT INPUT MESSAGES RETURN TO THIS <P>-->
这是我的Javascript代码片段,请注意,我只发布了else语句中的select菜单部分
var seating = '';
seating = document.getElementById('selectmenu').options[document.getElementById('selectmenu').selectedIndex].text.split('(')[0]
cost = parseInt(document.getElementById('selectmenu').value,10) * numTickets;//This calculates the cost(selectMenu*numTickets)
var Orderemail = document.getElementById('txtOrderEmail').value;//This will grab the email value Inputed.
OrderInput +=("Thanks " + ValidateOrderName + " for your order!<br /> <br />" + "Your details are: <br />" + "Your E-mail address is: " + Orderemail + "<br />" + newsletter + "<br /> <br />" +
"You have the following tickets reserved: <br />" + numTickets +" on " + prefNight + " and your seating is :" + seating + "<br /> <br />" + "The total cost of your order will be: $" + cost);
document.getElementById('OrderInput').innerHTML = OrderInput;//This prints the users details to a html element.
document.getElementById('orderErrorMsg').innerHTML = '';//Removes error messages when everything is correct.
}
return true;
}
你必须重新思考整个逻辑。引用单个选项的值没有用,而是引用select元素本身的值。包含所选选项的值。我不熟悉:\,是否有方法获取innertext.split@Teemu将此添加到flowstoneknight提供的答案中:.split[0]。您好,我将在哪里添加此函数?因为我有一个onClick函数,可以验证其他所有内容。非常感谢你!这对我帮助很大。。现在工作得很好。。。我将在下面发布正确的代码
var seating = '';
seating = document.getElementById('selectmenu').options[document.getElementById('selectmenu').selectedIndex].text.split('(')[0]
cost = parseInt(document.getElementById('selectmenu').value,10) * numTickets;//This calculates the cost(selectMenu*numTickets)
var Orderemail = document.getElementById('txtOrderEmail').value;//This will grab the email value Inputed.
OrderInput +=("Thanks " + ValidateOrderName + " for your order!<br /> <br />" + "Your details are: <br />" + "Your E-mail address is: " + Orderemail + "<br />" + newsletter + "<br /> <br />" +
"You have the following tickets reserved: <br />" + numTickets +" on " + prefNight + " and your seating is :" + seating + "<br /> <br />" + "The total cost of your order will be: $" + cost);
document.getElementById('OrderInput').innerHTML = OrderInput;//This prints the users details to a html element.
document.getElementById('orderErrorMsg').innerHTML = '';//Removes error messages when everything is correct.
}
return true;
}