隐藏/显示的最佳方法取决于jQuery的选择选项
Best way to hide/show depending on select options with jQuery?
我有一个选择,里面有两个选项。我希望默认选项在选择元素下方显示输入,而另一个选项在选择时隐藏该输入,而是显示一段文本。我尝试使用绑定和更改,但我无法让它工作。最好的方法是什么?
这是一个微小的、可重用的函数,可以减少显示/隐藏的一些代码混乱。
jQuery.fn.showIf = function (condition) {
return this[condition ? 'show' : 'hide']();
}
你像这样使用它:
$('#foo').change(function() {
$('#input').showIf(el.val() === "1");
$('#text').showIf(el.val() !== "1");
});
沿着这些思路的东西应该可以解决问题:
<select id="foo">
<option value="1">Enter a value</option>
<option value="2">Show some text</option>
</select>
<input id="input" type="text"/>
<p id="text" style="display:none">
Blah blah blah blah blah blah blah blah
blah blah blah blah blah blah blah blah
</p>
$('#foo').change(function() {
var el = $(this);
if(el.val() === "1") {
$('#input').show();
$('#text').hide();
} else {
$('#text').show();
$('#input').hide();
}
});
http://jsfiddle.net/J5Enp/
相关文章:
- 如何使用jQuery选择下拉列表的值
- 按照选项卡索引的顺序循环一个jQuery选择
- 使用Jquery选择或取消选择ListView中的所有复选框
- jQuery选择器无法正常工作
- 使用基于文本的 Jquery 选择选项
- 将jquery选择器转换为数组
- JavaScriptDOM正常工作时JQuery选择器不工作
- 在我的情况下,使用带有变量失败的 jquery 选择器
- 换行符插入jquery选择器
- 使用javascript"这个“;用于jquery选择器
- jQuery选择器缓存问题
- jQuery 选择器,所有跨度都在 DIV 内
- JQuery选择器:如果同级具有.class,则选择td
- jQuery选择器不识别任何动态创建的HTML输入函数
- 循环遍历元素jquery选择器
- jQuery选择器错误:无法识别的表达式
- 自定义jquery选择器属性未在模拟器中激发
- Jquery选择器为空,我应该使用哪个作用域
- jQuery选择JavaScript生成的元素
- Jquery选择菜单选项在页面刷新时失败