如果选择单选按钮“教授”,我该如何仅允许字段'name'和'email'显示?有人可以告诉我如何在jQuery中执行此操作吗?
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(':radio[name="addType"]').click(function() {
$('#name-and-email-container').toggle(':not(:radio[value="Professor"]:checked)');
});
</script>
<?php
require_once('inc/dbc1.php');
$pdo = new PDO('mysql:host=####;dbname=###', $username, $password);
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sth = $pdo->prepare('
SELECT name
FROM Department
;');
$sth->execute(array());
?>
<div id="popup_name" class="popup_block">
<h2 style="padding:0; margin:0;">Add a:</h2><br>
<form action="inc/add_p_c_validate.php" method="post" id="addition">
Professor<input type="radio" name="addType" value="Professor" />
Course<input type="radio" name="addType" value="Course" />
<div id="name-and-email-container">
<br><br>Name: <input type="text" name="name" /><br>
Department: <select name="deptName" id="deptName"><?php while($row = $sth->fetch(PDO::FETCH_ASSOC)) {echo "<option>".$row['name']." "."</option>";} ?></select>
Email: <input type="text" name="email" /><br></div><br>
<input type="submit" name="submit" />
</form>
</div><!--popup_name-->
答案 0 :(得分:3)
目前使用您当前的HTML会很困难,特别是因为您的标签不在label
元素中。
而是将名称和电子邮件输入及其各自的标签包装在自己的元素中。
$(':radio[name="addType"]').click(function() {
$('#name-and-email-container').toggle(':not(:radio[value="Professor"]:checked)');
});