使用表单选择菜单在HTML中显示条件字段

时间:2014-03-16 19:37:07

标签: javascript jquery html forms

我正在尝试让我的页面显示不同的表单元素,具体取决于从选择菜单中选择的选项,但我无法让它为我工作

我的HTML是

 <h3>Register For General Admission</h3>

<form action="GeneralRegister" method="POST" style="text-align:center">
Type Of Ticket: <select name="typeofticket" size="1" id="generalAdmissionList">
    <option value="1"></option>
    <option value="1">General Admission</option>
    <option value="1">General Admission School</option>
</select>
<br>
<div id="1">
    <!--for general admission-->
    First name:
    <input type="text" name="firstname">
    <br>Last name:
    <input type="text" name="lastname">
    <br>
</div>
<div id="2">
    <!--For Schools-->
    School name:
    <input type="text" name="school">
    <br>
</div>
Email:
<input type="text" name="email">
<br>
<input type="submit" value="Submit">
<br>
</form>

我的JavaScript是

$("#generalAdmissionList").change(function () {
var selected = $("#generalAdmissionList option:selected").form();
$('div').hide();
$('#' + selected).show();
});

$(document).ready(function (e) {
$('div').hide();
});

任何帮助都会很棒

1 个答案:

答案 0 :(得分:3)

将您的选择选项更改为此

<option value="1">General Admission</option>
<option value="2">General Admission School</option>

和获取所选值的javascript行应该是这样的

var selected = $("#generalAdmissionList option:selected").val();