我的页面中有两个下拉列表,我根据其他值设置了一个下拉列表的可见性,如下所示:
$(document).ready(function () {
if (($("#response option:selected").val() == "A") || ($("#response option:selected").val() == 'B')) {
$("#additional").show();
$(document).scrollTo('#additional');
}
$('#response').change(function () {
$("#response option:selected").val();
if (($("#response option:selected").val() == "A") || ($("#response option:selected").val() == 'B')) {
$("#additional").show();
} else {
$("#additional").hide();
}
});
});
----------------------------------------------------------
<div id="response" class="col-lg-4">
<select>
<option value="">Please Select</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
</div>
<div id="additional" style="display: block;">
<select><option value="00">Please Select </option></select>
</div>
这不起作用,我收到一个控制台错误,如“jquery 1.10.2 - 未捕获错误:语法错误,无法识别的表达式:unsupported pseudo:select”。可能是什么问题?
答案 0 :(得分:0)
仅选择val而非选项
$('#response select').val()
答案 1 :(得分:0)
:selected
是一个错字。您可以使用:select
伪选择器,但我建议您使用val()
。另外,我建议存储您正在重复使用的上下文:
$(document).ready(function () {
var responseSelect = $("#response select");
var additional = $("#additional");
if ((responseSelect.val() == "A") || (responseSelect.val() == 'B')) {
additional.show();
$(document).scrollTo(additional);
}
responseSelect.change(function () {
var currentVal = $(this).val();
if ((currentVal == "A") || (currentVal == 'B')) {
additional.show();
} else {
additional.hide();
}
});
});