好的,我正在尝试编写一个脚本,根据用户选择显示和隐藏项目。我能够弄清楚如何使用无线电组这样做,但是当涉及到一个选择项目时,我无法弄明白。任何人都知道我在这里做错了什么?
我的jQuery
$('select#contact').change(function() {
var isPhone = $('#phone').is(':selected');
var isEmail = $('#email').is(':selected');
$('#cell').toggleClass('req', isPhone);
if ( isPhone === true ) {
$('#phoneNum').animate({'height':'show'}, 500);
} else if ( isPhone === false ){
$('#phoneNum').animate({'height':'hide'}, 200);
}
$('#Remail').toggleClass('req', isEmail);
if ( isEmail === true ) {
$('#emailAdd').animate({'height':'show'}, 500);
} else if ( isEmail === false ){
$('#emailAdd').animate({'height':'hide'}, 200);
}
});
我的HTML
<p>
<label for="contact">How Would You Like Us To Contact You?</label>
<select id="contact" name="contact" class="req">
<option></option>
<option id="phone">By Phone</option>
<option id="email">By Email</option>
</select>
</p>
<p id="phoneNum">
<label for="cell">Best Phone Number</label>
<input id="cell" name="cell" type="tel" placeholder="e.g. 555-555-5555" class=""/>
</p>
<p id="emailAdd">
<label for="Remail">Email</label>
<input id="Remail" name="Remail" placeholder="something@something.com" type="email" class=""/>
</p>
答案 0 :(得分:0)
想出来:
$('select#contact').change(function() {
var isPhone = $(this).find("option:selected").is('#phone');
var isEmail = $(this).find("option:selected").is('#email');
$('#cell').toggleClass('req', isPhone);
if ( isPhone === true ) {
$('#phoneNum').animate({'height':'show'}, 500);
} else if ( isPhone === false ){
$('#phoneNum').animate({'height':'hide'}, 200);
}
$('#Remail').toggleClass('req', isEmail);
if ( isEmail === true ) {
$('#emailAdd').animate({'height':'show'}, 500);
} else if ( isEmail === false ){
$('#emailAdd').animate({'height':'hide'}, 200);
}
});