根据选择框中的选项值显示/隐藏字段

时间:2012-11-21 19:35:00

标签: javascript jquery drop-down-menu

我正在尝试使用稍微修改过的this code版本来根据行程类型显示/隐藏this page上的字段返回时间:

单向:隐藏返回时间

往返:显示返回时间

这是Select Box html。代码由wordpress上的cforms自动生成。

<select name="TripType" id="TripType" class="cformselect" >
                <option value="One Way">One Way</option>
                <option value="Round Trip">Round Trip</option>
            </select>

这是我试图隐藏的表单元素:

<li id="li--8" class=""><label id="label--8" for="DepartLeg2-Time"><span>Return Time</span></label><select name="DepartLeg2-Time" id="DepartLeg2-Time" class="cformselect" >
                    <option value="12:00am">12:00am</option>
                    <option value="12:30am">12:30am</option>
                    <option value="1:00am">1:00am</option>
                    <option value="1:30am">1:30am</option>
                    <option value="2:00am">2:00am</option>
                    <option value="2:30am">2:30am</option>
                    <option value="3:00am">3:00am</option>
                    <option value="3:30am">3:30am</option>
                    <option value="4:00am">4:00am</option>
                    <option value="4:30am">4:30am</option>
                    <option value="5:00am">5:00am</option>
                    <option value="5:30am">5:30am</option>
                    <option value="6:00am">6:00am</option>
                    <option value="6:30am">6:30am</option>
                    <option value="7:00am">7:00am</option>
                    <option value="7:30am">7:30am</option>
                    <option value="8:00am">8:00am</option>
                    <option value="8:30am">8:30am</option>
                    <option value="9:00am">9:00am</option>
                    <option value="9:30am">9:30am</option>
                    <option value="10:00am">10:00am</option>
                    <option value="10:30am">10:30am</option>
                    <option value="11:00am">11:00am</option>
                    <option value="11:30am">11:30am</option>
                    <option value="12:00pm">12:00pm</option>
                    <option value="12:30pm">12:30pm</option>
                    <option value="1:00pm">1:00pm</option>
                    <option value="1:30pm">1:30pm</option>
                    <option value="2:00pm">2:00pm</option>
                    <option value="2:30pm">2:30pm</option>
                    <option value="3:00pm">3:00pm</option>
                    <option value="3:30pm">3:30pm</option>
                    <option value="4:00pm">4:00pm</option>
                    <option value="4:30pm">4:30pm</option>
                    <option value="5:00pm">5:00pm</option>
                    <option value="5:30pm">5:30pm</option>
                    <option value="6:00pm">6:00pm</option>
                    <option value="6:30pm">6:30pm</option>
                    <option value="7:00pm">7:00pm</option>
                    <option value="7:30pm">7:30pm</option>
                    <option value="8:00pm">8:00pm</option>
                    <option value="8:30pm">8:30pm</option>
                    <option value="9:00pm">9:00pm</option>
                    <option value="9:30pm">9:30pm</option>
                    <option value="10:00pm">10:00pm</option>
                    <option value="10:30pm">10:30pm</option>
                    <option value="11:00pm">11:00pm</option>
                    <option value="11:30pm">11:30pm</option>
                </select></li>

这是我正在使用的javascript:

$("#TripType").change(function() {
  $("#li--8")[$(this).val() == "Round Trip" ? 'show' : 'hide']("fast");
}).change();

我在JSFiddle:http://jsfiddle.net/MS4Ck/上有代码。但是,它不适用于该网站。我的控制台没有给我任何错误,除了来自我放在那里的一些日志代码的引用错误,以确保调用javascript(它是)。有任何想法吗?非常感谢你!

1 个答案:

答案 0 :(得分:1)

只是猜测,但请确保此函数包含在docReady函数中,例如:

$(document).ready(function() {
    $("#TripType").change(function() {
        $("#li--8")[$(this).val() == "Round Trip" ? 'show' : 'hide']("fast");
    }).change();
});