![](/img/trans.png)
[英]Show/Hide TextBox and enable/disable Textbox validation based on CheckBoxList selection
[英]How to hide a checkboxlist based on a dropdown selection using JQuery?
我有一个带有下拉列表的表单。 根据下拉列表中的选定项目,将出现相应的复选框列表,其他复选框列表将消失。 您如何使用JQuery完成此操作?
这是您应该能够轻松适应特定元素的Javascript:
$('#dropdownlist').on('change', function () {
if ($(this).val()) {
if($(this).val() === "some value") {
$('#somecheckboxgroup').show();
$('#someothercheckboxgroup').hide();
}
else if($(this).val() === "some other value") {
$('#somecheckboxgroup').hide();
$('#someothercheckboxgroup').show();
}
}
});
本质上,您只想在下拉列表每次更改时运行一个函数,然后在其中检查当前选定的值,然后根据观察到的值运行所需的代码。
这是一个非常基本的示例-http://jsfiddle.net/jayblanchard/G8z3r/可以通过使用不同的选择器,id和类来缩短代码,但是我想给您一个基本的思路。
$('select[name="showbox"]').change(function() {
if('foo' == $(this).val() ) {
$('div').hide(); // make sure all divs are hidden
$('#checkboxA').show(); // show the right one
} else if ('bar' == $(this).val() ) {
$('div').hide(); // make sure all divs are hidden
$('#checkboxB').show(); // show the right one
} else if ('both' == $(this).val() ) {
$('div').show(); // sow all divs
} else {
$('div').hide();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.