我是新來的jQuery,我想知道是否有寫如下的代碼,將顯示與該按鈕相關的表單域及隱藏其他兩個表單字段一個更好的,更有效的方式是點擊一個時:如何簡化這個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全部關閉,則更改頁腳border-color。它只是在加載時將邊框顏色變成黑色,然後再也不會再做。
編輯:下面是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的!
如果你覺得有必要,你可以通過選擇相結合,像這樣開始:' $('button [name =「parents」],button [name =「teachers」],button [name =「kids」]')'然後切換e.currentTarget()。attr('name')'where需要。 – soulseekah
如果明確地向'
如果分組,您可以進一步設置所有」slideUp「調用的值將它們全部設置爲300,然後將被調用的值設置爲0並將它滑動。但請確保你不過分。 – soulseekah