从下拉列表中选择选项时,Javascript 显示隐藏的 div
我在表单上有 5 个下拉列表,在这些列表下有需要用户输入的 DIV 元素。我显示第一个下拉列表的第一个 DIV,并使用 style="display: none;" 隐藏所有其他表单。
我正在寻找一个 Javascript,当我从第二个下拉列表中选择任何选项时,它将显示第二个 DIV。
任何帮助将不胜感激。
提前致谢, 瑞安·史密斯
<tr>
<td>Destination 1</td><td>Destination 2</td><td>Destination 3</td><td>Destination 4</td><td>Destination 5</td>
</tr>
<tr>
<td>
<SELECT NAME=drop1 style="width:150px;">
<OPTION VALUE=0>
<?=$optionsdest?>
</SELECT>
</td>
<td>
<SELECT NAME=drop2 style="width:150px;" onChange="javascript to show DIV leg1b">
<OPTION VALUE=1><?=$optionsdest?>
</SELECT>
</td>
<td>
<SELECT NAME=drop3 style="width:150px;" onChange="javascript to show DIV leg1c">
<OPTION VALUE=0>0
<OPTION VALUE=1>1
</SELECT>
</td>
<td>
<SELECT NAME=drop4 style="width:150px;" onChange="javascript to show DIV leg1d">
<OPTION VALUE=0>
<?=$optionsdest?>
</SELECT>
</td>
<td>
<SELECT NAME=drop5 style="width:150px;" onChange="javascript to show DIV leg1e">
<OPTION VALUE=0>
<?=$optionsdest?>
</SELECT>
</td>
</tr>
<tr>
<td>
Arrive Drop 1
<br>
<input type="date" id="arrivedatetime1" name="arrivedatetime1" value=<?PHP echo $arrivedate ?>>
<br>
<SELECT NAME=arrivetime1 style="width:150px;">
<OPTION VALUE=0>
<?=$optionstime?>
</SELECT>
<br>
Depart Drop 1
<br>
<input type="date" id="departdatetime1" name="departdatetime1" value=<?PHP echo $departdate ?>>
<br>
<SELECT NAME=departtime1 style="width:150px;">
<OPTION VALUE=0>
<?=$optionstime?>
</SELECT>
</td>
<td>
<div id='legb1' style="display: none;">
Arrive Drop 2
<br>
<input type="date" id="arrivedatetime1" name="arrivedatetime1" value=<?PHP echo $arrivedate ?>>
<br>
<SELECT NAME=arrivetime1 style="width:150px;">
<OPTION VALUE=0>
<?=$optionstime?>
</SELECT>
<br>
Depart Drop 2
<br>
<input type="date" id="departdatetime1" name="departdatetime1" value=<?PHP echo $departdate ?>>
<br>
<SELECT NAME=departtime1 style="width:150px;">
<OPTION VALUE=0>
<?=$optionstime?>
</SELECT>
</div>
</td>
<td>
<div id='legc1' style="display: none;">
Arrive Drop 3
<br>
<input type="date" id="arrivedatetime1" name="arrivedatetime1" value=<?PHP echo $arrivedate ?>>
<br>
<SELECT NAME=arrivetime1 style="width:150px;">
<OPTION VALUE=0>
<?=$optionstime?>
</SELECT>
<br>
Depart Drop 3
<br>
<input type="date" id="departdatetime1" name="departdatetime1" value=<?PHP echo $departdate ?>>
<br>
<SELECT NAME=departtime1 style="width:150px;">
<OPTION VALUE=0>
<?=$optionstime?>
</SELECT>
</div>
</td>
<td>
<div id='legd1' style="display: none;">
Arrive Drop 4
<br>
<input type="date" id="arrivedatetime1" name="arrivedatetime1" value=<?PHP echo $arrivedate ?>>
<br>
<SELECT NAME=arrivetime1 style="width:150px;">
<OPTION VALUE=0>
<?=$optionstime?>
</SELECT>
<br>
Depart Drop 4
<br>
<input type="date" id="departdatetime1" name="departdatetime1" value=<?PHP echo $departdate ?>>
<br>
<SELECT NAME=departtime1 style="width:150px;">
<OPTION VALUE=0>
<?=$optionstime?>
</SELECT>
</div>
</td>
<td>
<div id='lege1' style="display: none;">
Arrive Drop 5
<br>
<input type="date" id="arrivedatetime1" name="arrivedatetime1" value=<?PHP echo $arrivedate ?>>
<br>
<SELECT NAME=arrivetime1 style="width:150px;">
<OPTION VALUE=0>
<?=$optionstime?>
</SELECT>
<br>
Depart Drop 5
<br>
<input type="date" id="departdatetime1" name="departdatetime1" value=<?PHP echo $departdate ?>>
<br>
<SELECT NAME=departtime1 style="width:150px;">
<OPTION VALUE=0>
<?=$optionstime?>
</SELECT>
</td>
</tr>
I have 5 drop down lists on a Form, under which I have DIV elements that require user input. I display the first DIV for the first drop down list and hide all the other forms using style="display: none;".
I am looking for a Javascript that will show the second DIV when I select any option from the 2nd drop down list.
Any assistance would be appreciated.
Thanks in advance,
Ryan Smith
<tr>
<td>Destination 1</td><td>Destination 2</td><td>Destination 3</td><td>Destination 4</td><td>Destination 5</td>
</tr>
<tr>
<td>
<SELECT NAME=drop1 style="width:150px;">
<OPTION VALUE=0>
<?=$optionsdest?>
</SELECT>
</td>
<td>
<SELECT NAME=drop2 style="width:150px;" onChange="javascript to show DIV leg1b">
<OPTION VALUE=1><?=$optionsdest?>
</SELECT>
</td>
<td>
<SELECT NAME=drop3 style="width:150px;" onChange="javascript to show DIV leg1c">
<OPTION VALUE=0>0
<OPTION VALUE=1>1
</SELECT>
</td>
<td>
<SELECT NAME=drop4 style="width:150px;" onChange="javascript to show DIV leg1d">
<OPTION VALUE=0>
<?=$optionsdest?>
</SELECT>
</td>
<td>
<SELECT NAME=drop5 style="width:150px;" onChange="javascript to show DIV leg1e">
<OPTION VALUE=0>
<?=$optionsdest?>
</SELECT>
</td>
</tr>
<tr>
<td>
Arrive Drop 1
<br>
<input type="date" id="arrivedatetime1" name="arrivedatetime1" value=<?PHP echo $arrivedate ?>>
<br>
<SELECT NAME=arrivetime1 style="width:150px;">
<OPTION VALUE=0>
<?=$optionstime?>
</SELECT>
<br>
Depart Drop 1
<br>
<input type="date" id="departdatetime1" name="departdatetime1" value=<?PHP echo $departdate ?>>
<br>
<SELECT NAME=departtime1 style="width:150px;">
<OPTION VALUE=0>
<?=$optionstime?>
</SELECT>
</td>
<td>
<div id='legb1' style="display: none;">
Arrive Drop 2
<br>
<input type="date" id="arrivedatetime1" name="arrivedatetime1" value=<?PHP echo $arrivedate ?>>
<br>
<SELECT NAME=arrivetime1 style="width:150px;">
<OPTION VALUE=0>
<?=$optionstime?>
</SELECT>
<br>
Depart Drop 2
<br>
<input type="date" id="departdatetime1" name="departdatetime1" value=<?PHP echo $departdate ?>>
<br>
<SELECT NAME=departtime1 style="width:150px;">
<OPTION VALUE=0>
<?=$optionstime?>
</SELECT>
</div>
</td>
<td>
<div id='legc1' style="display: none;">
Arrive Drop 3
<br>
<input type="date" id="arrivedatetime1" name="arrivedatetime1" value=<?PHP echo $arrivedate ?>>
<br>
<SELECT NAME=arrivetime1 style="width:150px;">
<OPTION VALUE=0>
<?=$optionstime?>
</SELECT>
<br>
Depart Drop 3
<br>
<input type="date" id="departdatetime1" name="departdatetime1" value=<?PHP echo $departdate ?>>
<br>
<SELECT NAME=departtime1 style="width:150px;">
<OPTION VALUE=0>
<?=$optionstime?>
</SELECT>
</div>
</td>
<td>
<div id='legd1' style="display: none;">
Arrive Drop 4
<br>
<input type="date" id="arrivedatetime1" name="arrivedatetime1" value=<?PHP echo $arrivedate ?>>
<br>
<SELECT NAME=arrivetime1 style="width:150px;">
<OPTION VALUE=0>
<?=$optionstime?>
</SELECT>
<br>
Depart Drop 4
<br>
<input type="date" id="departdatetime1" name="departdatetime1" value=<?PHP echo $departdate ?>>
<br>
<SELECT NAME=departtime1 style="width:150px;">
<OPTION VALUE=0>
<?=$optionstime?>
</SELECT>
</div>
</td>
<td>
<div id='lege1' style="display: none;">
Arrive Drop 5
<br>
<input type="date" id="arrivedatetime1" name="arrivedatetime1" value=<?PHP echo $arrivedate ?>>
<br>
<SELECT NAME=arrivetime1 style="width:150px;">
<OPTION VALUE=0>
<?=$optionstime?>
</SELECT>
<br>
Depart Drop 5
<br>
<input type="date" id="departdatetime1" name="departdatetime1" value=<?PHP echo $departdate ?>>
<br>
<SELECT NAME=departtime1 style="width:150px;">
<OPTION VALUE=0>
<?=$optionstime?>
</SELECT>
</td>
</tr>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个示例 - 我将与名称相同的 ID 添加到选择
http://jsfiddle.net/mplungjan/ 32Ukf/
纯 JS
Here is an example - I added the same ID as the name to the selects
http://jsfiddle.net/mplungjan/32Ukf/
Plain JS
我会通过将选项的
value
设置为您要显示的div
的ID
来稍微更改您的标记。下面的代码应该可以帮助您朝着正确的方向前进。然后用这个方法处理它:
这是一个工作小提琴。
I would change your markup a bit by setting the
value
of the option to theID
of thediv
you want to display. The code below should get you going in the right direction.Then handle it with this method:
Here's a working fiddle.