使用.change()将addClass与选择列表一起使用

时间:2011-08-08 16:32:21

标签: onchange

我正在尝试通过在下拉列表中选择“其他”选项时添加类来创建隐藏字段。但我不太确定这样做的正确方法。

我隐藏了输入,当选择了选项时,我想添加类“视图”,它会添加显示块,使隐藏字段可见。

这是一个小提示,显示我到目前为止所提供的任何帮助将非常感谢:http://jsfiddle.net/maikunari/NX795/

1 个答案:

答案 0 :(得分:1)

$(document).ready(function(){
    $("#select-box").change(function(){
        if($(this).val() == "other"){
            $("#text-field").show();        
        } else {
            $("#text-field").hide();    
        }
    });
});

<select id="select-box">
<option value="Email Newsletter">Email Newsletter</option>
<option value="Yellow Pages ">Yellow Pages </option>
<option id="other-select" value="other">Other</option>
</select>