我正在尝试创建一个下拉字段,如果选中"其他"项,则会出现"请指定"文本框.下面只使用一个下拉选择字段,但只要我添加第二个选择字段,它就不再有效.
这是我所拥有的似乎不起作用的东西:
CSS ...
#techother{display:none;}
#lmsother{display:none;}
Run Code Online (Sandbox Code Playgroud)
身体...
<p>Chose Your Browser: <select name = "Browser" required>
<option value = "">-- Select an Option --</option>
<option value = "1">IE</option>
<option value = "2">FF</option>
<option value = "3">Safari</option>
<option value = "4">Opera</option>
<option value = "5">Other</option>
</select>
</p>
<div id="browserother">
<p>Please Specify: <label id="browserlabel"><input name="Other Browser" type="text" placeholder="Other Browser" size="50" /></label></p>
</div>
<p>Operating System: <select name = "OS" required>
<option value = "">-- Select an Option --</option>
<option value = "Win">Windows</option>
<option value = "ios">iOS</option>
<option value = "otheros">Other</option>
</select>
</p>
<div id="osother">
<p>Please Specify: <label id="oslabel"><input name="Other OS" type="text" placeholder="Other OS" size="50" /></label></p>
</div>
Run Code Online (Sandbox Code Playgroud)
脚本...
<script>
$('form select[name=Browser]').change(function(){
if ($('form select option:selected').val() == '5'){
$('#browserother').show();
}else{
$('#browserother').hide();
}
});
$('form select[name=OS]').change(function(){
if ($('form select option:selected').val() == 'otheros'){
$('#osother').show();
}else{
$('#osother').hide();
}
});
</script>
Run Code Online (Sandbox Code Playgroud)
有什么办法使这项工作?谢谢!
试试这个演示:http://jsfiddle.net/2pM3n/1/
$('select[name=Browser]').change(function () {
if ($(this).val() == '5') {
$('#browserother').show();
} else {
$('#browserother').hide();
}
});
$('select[name=OS]').change(function () {
if ($(this).val() == 'otheros') {
$('#osother').show();
} else {
$('#osother').hide();
}
});
Run Code Online (Sandbox Code Playgroud)
你去了:http : //jsfiddle.net/ecZrE/
您在选择器中混合了一些东西。使用
$('p select[name=Browser]')
Run Code Online (Sandbox Code Playgroud)
代替
$('form select[name=Browser]')
Run Code Online (Sandbox Code Playgroud)
因为没有表单元素。但是您的代码段还是不完整。
另一个错误的选择器是
$('form select option:selected')
Run Code Online (Sandbox Code Playgroud)
因为您忘记了指定select元素。