在jquery中只针对一个选择,而不是全部
Targeting only one select in jquery not all of them
在
jquery中仅针对一个切换开关(选择后的下一个div),而不是全部
这有效,除了当我只希望它只显示它下面的一个时,它会显示所有切换。
这是 html
$( ".toggle" ).hide();
$(function () {
$(".binary").change(function() {
if ($(this).val() == "0"){
$( ".toggle" ).hide();
} else {
$( ".toggle" ).show();
}
$("#pane").customScrollbar("resize", true);
});
});
这是 html
<div class="option checkbox">
<label class="field_wrap">
<div class="label">1</div>
<div class="field">
<select class="binary" name="option_1">
<option value="0"></option>
<option value="1"></option>
</select>
</div>
</label>
</div>
<div class="toggle">
toggle 1
</div>
<div class="option checkbox">
<label class="field_wrap">
<div class="label">1</div>
<div class="field">
<select class="binary" name="option_1">
<option value="0"></option>
<option value="1"></option>
</select>
</div>
</label>
</div>
<div class="toggle">
toggle 2
</div>
你应该试试:first选择器
$(function () {
$(".binary:first").change(function() {
if($(this).val() == "0"){
$(this).next(".toggle").hide();
} else {
$(this).next(".toggle").show();
}
$("#pane").customScrollbar("resize", true);
});
});
请尝试以下操作
$( ".toggle" ).hide();
$(function () {
$(".binary").change(function() {
if ($(this).val() == "0"){
$(this).parent().parent().parent().next().hide();
} else {
$(this).parent().parent().parent().next().show();
}
$("#pane").customScrollbar("resize", true);
});
});
这与@Ganesh的答案类似,但具有更高的特异性,这在你的方案中可能很重要。
$(".toggle").hide();
$(function () {
$(".binary").change(function () {
if ($(this).val() == "0") {
console.log($(this).closest('div.option.checkbox').length);
$(this).closest('div.option.checkbox').next(".toggle").hide();
} else {
$(this).closest('div.option.checkbox').next(".toggle").show();
}
$("#pane").customScrollbar("resize", true);
});
});
你可以使用 jQuery 的 .next() 方法。
$(function () {
$(".binary").change(function() {
if($(this).val() == 0){
$(this).parents("div.checkbox").next('.toggle').hide();
} else {
$(this).parents("div.checkbox").next('.toggle').show();
}
$("#pane").customScrollbar("resize", true);
});
});
这是一个演示小提琴。
相关文章:
- 从a-z中随机选择一个字母
- AngularJS ng选项在选择一个选项后删除了默认的空白值
- 在下拉列表中选择一个选项的值
- 在类JavaScript选择的另一个元素中选择一个标记元素
- Jquery:当两个或多个条件为true时,选择一个元素
- 可以使用createRange选择一个空范围
- 选择一个元素,使其最近的具有特殊属性的父元素具有特殊值
- 使用 Jquery 选择一个 td 元素
- 当使用jQuery/Javascript选择一个多选时,如何使所有值都不被选中:
- 在一组复选框中,一次只允许选择一个
- 如何强制用户从下拉列表和按钮中选择一个值以启用保存按钮
- 从数组中选择一个变量
- 正在读取<p: 选择一个菜单/>在onchange回调中
- 在输入字段中选择一个值,然后用相应的数据填充另一个字段
- 如何以编程方式在 jsTree 中选择一个节点并打开所有父节点
- Asp.net 引导下拉菜单 - 选择一个项目
- 如何根据 jsp 中数据库中的值在下拉列表中选择一个值
- 通过硒选择一个javascript下拉列表
- 选择一个从 mysql 返回选中/未选中复选框的对象
- 如何在 PHP 中使用 OnChange 事件的下拉列表中选择一个选项后,对大量数据行设置分页