我是 jQuery 新手,想知道是否有更好、更有效的方法来编写以下代码,该代码将显示与按钮关联的表单字段,并在单击一个时隐藏其他两个表单字段:
$(document).ready(function() {
var frm = $('form');
var comp = $('form[name="parents"]');
var comt = $('form[name="teachers"]');
var comk = $('form[name="kids"]');
$(frm).hide();
$('button[name="parents"]').click(function(e) {
$(comp).slideToggle(300);
$(comt).slideUp(300);
$(comk).slideUp(300);
$('footer').css("border-color","#2dff1f");
e.preventDefault();
});
$('button[name="teachers"]').click(function(e) {
$(comt).slideToggle(300);
$(comp).slideUp(300);
$(comk).slideUp(300);
$('footer').css("border-color","#23a4f2");
e.preventDefault();
});
$('button[name="kids"]').click(function(e) {
$(comk).slideToggle(300);
$(comt).slideUp(300);
$(comp).slideUp(300);
$('footer').css("border-color","#9800ff");
e.preventDefault();
});
if ($("form:visible").length === 0) {
$('footer').css("border-color","#000");
}
});
此外,从底部开始的四行我尝试设置一个 if 语句,说如果 comp、comk、comt 都关闭,则更改页脚边框颜色。它只是在加载时将边框颜色变为黑色,然后再也不这样做了。
编辑:这是html结构-
<button name="parents">Parents</button>
<form name="parents" action="share.php" method="post">
<fieldset>
<span style="white-space:nowrap">
<label for="name">Name</label>
<input type="text" name="name" id="name" tabindex="1"></input>
</span>
<span style="white-space:nowrap">
<label for="email">Email</label>
<input type="text" id="email" name="email" tabindex="2" onchange="emailValidation()" class="textInput validateEmail required">
</span>
<span style="white-space:nowrap">
<label for="country">Country</label>
<select name="country" id="country" tabindex="3">
<optgroup label="Select One">
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
</optgroup>
</select>
<!--<label for="country">Country</label>
<?php require('countries.php'); ?> -->
</span>
<span style="white-space:nowrap">
<label for="zip">Zip Code</label>
<input type="text" id="zip" name="zip" tabindex="4" class="textInput required" maxlength="5">
</span>
<label for="message">Message</label>
<textarea name="message" id="message" tabindex="5"></textarea>
<input type="submit" tabindex="6" value="share with us">
</fieldset>
</form>
<button name="teachers">Teachers</button>
<form name="teachers" action="share.php" method="post">
<fieldset>
<span style="white-space:nowrap">
<label for="name">Name</label>
<input type="text" name="name" id="name" tabindex="1"></input>
</span>
<span style="white-space:nowrap">
<label for="email">Email</label>
<input type="text" id="email" name="email" tabindex="2" onchange="emailValidation()" class="textInput validateEmail required">
</span>
<span style="white-space:nowrap">
<label for="country">Country</label>
<select name="country" id="country" tabindex="3">
<optgroup label="Select One">
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
</optgroup>
</select>
<!--<label for="country">Country</label>
<?php require('countries.php'); ?> -->
</span>
<span style="white-space:nowrap">
<label for="zip">Zip Code</label>
<input type="text" id="zip" name="zip" tabindex="4" class="textInput required" maxlength="5">
</span>
<label for="message">Message</label>
<textarea name="message" id="message" tabindex="5"></textarea>
<input type="submit" tabindex="6" value="share with us">
</fieldset>
</form>
<button name="kids">Kids</button>
<form name="kids" action="share.php" method="post">
<fieldset>
<span style="white-space:nowrap">
<label for="name">Name</label>
<input type="text" name="name" id="name" tabindex="1"></input>
</span>
<span style="white-space:nowrap">
<label for="email">Email</label>
<input type="text" id="email" name="email" tabindex="2" onchange="emailValidation()" class="textInput validateEmail required">
</span>
<span style="white-space:nowrap">
<label for="country">Country</label>
<select name="country" id="country" tabindex="3">
<optgroup label="Select One">
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
<option>USA</option>
</optgroup>
</select>
<!--<label for="country">Country</label>
<?php require('countries.php'); ?> -->
</span>
<span style="white-space:nowrap">
<label for="zip">Zip Code</label>
<input type="text" id="zip" name="zip" tabindex="4" class="textInput required" maxlength="5">
</span>
<label for="message">Message</label>
<textarea name="message" id="message" tabindex="5"></textarea>
<input type="submit" tabindex="6" value="share with us">
</fieldset>
</form>
感谢您帮助我了解有关 jQuery 的更多信息!