如果有多个复选框,为什么只有一个复选框实例有效?
Why does only one instance of checkbox work if there are multiple
我有一个应用程序,它的多个实例存在于同一个页面中。我在应用程序中有一种幻灯片,它使用input
来显示相应的数据。然而,label
和input
只适用于页面中的一个实例。因此,如果页面中有两个相同应用程序的实例,则只有第一个实例有效。
$.fn.MP = function(options) {
var settings = $.extend({
// These are the defaults
text: ""
}, options);
$(this).on("click tap", "input", function() {
$(this).parent().siblings().find("input").prop("checked", false);
});
};
$("#item1").MP({});
$("#item2").MP({});
input[type="checkbox"] {
position: absolute;
opacity: 0;
}
.panelTop > div div {
display: none;
}
.panelTop > div input[type="checkbox"]:checked ~ div {
display: block;
}
.panel {
width: 400px;
height: 100px;
border: 1px solid black;
}
.panel > .panelTop {
width: 100%;
height: 49px;
border-bottom: 1px dashed black;
}
.panel > .panelBottom {
width: 100%;
height: 50px;
}
.panel > .panelTop > div div {
width: 24px;
height: 24px;
float: right;
margin: 0px 9px 0 0;
border: 1px dashed black;
}
.panel > .panelBottom > div {
width: 32px;
height: 32px;
float: right;
margin: 9px 9px 0 0;
border: 1px solid black;
text-align: center;
font-size: 22px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="panel" id="item1">
<div class="panelTop">
Dependant Buttons:
<div>
<input id="box-1" type="checkbox" checked>
<div>1.1</div>
<div>1.2</div>
<div>1.3</div>
</div>
<div>
<input id="box-2" type="checkbox">
<div>2.1</div>
<div>2.2</div>
<div>2.3</div>
</div>
<div>
<input id="box-3" type="checkbox">
<div>3.1</div>
<div>3.2</div>
<div>3.3</div>
</div>
</div>
<div class="panelBottom">
Main Buttons:
<label for="box-1" class="btn1">1</label>
<label for="box-2" class="btn2">2</label>
<label for="box-3" class="btn3">3</label>
</div>
</div>
<div class="panel" id="item2">
<div class="panelTop">
Dependant Buttons:
<div>
<input id="box-1" type="checkbox">
<div>1.1</div>
<div>1.2</div>
<div>1.3</div>
</div>
<div>
<input id="box-2" type="checkbox">
<div>2.1</div>
<div>2.2</div>
<div>2.3</div>
</div>
<div>
<input id="box-3" type="checkbox">
<div>3.1</div>
<div>3.2</div>
<div>3.3</div>
</div>
</div>
<div class="panelBottom">
Main Buttons:
<label for="box-1" class="btn1">1</label>
<label for="box-2" class="btn2">2</label>
<label for="box-3" class="btn3">3</label>
</div>
</div>
因此,正如您在演示中看到的,有两个应用程序实例,即使单击第二个实例,单击1, 2 or 3
的按钮也会在第一个实例中发生更改。
我想使每个实例正确工作,每个单选按钮只在按下时才工作。
任何想法?
@dryden-long已经提到,id必须是唯一的。解决这个问题,并相应地修改相应的for
属性,你的问题就解决了。
编辑:根据您的评论,由于您的div.panel
元素的id
值已经是唯一的,您可以做以下事情,它为您的input
元素动态生成唯一的id
值,并相应地更新相应的label
元素的for
值:
$.fn.MP = function(options) {
var settings = $.extend({
// These are the defaults
text: ""
}, options);
$(this).on("click tap", "input", function() {
$(this).parent().siblings().find("input").prop("checked", false);
});
};
// dynamically-generate id/for values
$('div.panel').each(function(index, el) {
var $panel = $(el);
var panelId = $panel.attr('id');
var $panelBottom = $('div.panelBottom', $panel);
$('div.panelTop > div > input', $panel).each(function(indexInput, elInput) {
var $input = $(elInput);
var inputId = panelId + '-box-' + indexInput;
$input.attr('id', inputId);
$('label:nth(' + indexInput + ')', $panelBottom).attr('for', inputId);
});
});
$("#item1").MP({});
$("#item2").MP({});
input[type="checkbox"] {
position: absolute;
opacity: 0;
}
.panelTop > div div {
display: none;
}
.panelTop > div input[type="checkbox"]:checked ~ div {
display: block;
}
.panel {
width: 400px;
height: 100px;
border: 1px solid black;
}
.panel > .panelTop {
width: 100%;
height: 49px;
border-bottom: 1px dashed black;
}
.panel > .panelBottom {
width: 100%;
height: 50px;
}
.panel > .panelTop > div div {
width: 24px;
height: 24px;
float: right;
margin: 0px 9px 0 0;
border: 1px dashed black;
}
.panel > .panelBottom > div {
width: 32px;
height: 32px;
float: right;
margin: 9px 9px 0 0;
border: 1px solid black;
text-align: center;
font-size: 22px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="panel" id="item1">
<div class="panelTop">
Dependant Buttons:
<div>
<input type="checkbox">
<div>1.1</div>
<div>1.2</div>
<div>1.3</div>
</div>
<div>
<input type="checkbox">
<div>2.1</div>
<div>2.2</div>
<div>2.3</div>
</div>
<div>
<input type="checkbox">
<div>3.1</div>
<div>3.2</div>
<div>3.3</div>
</div>
</div>
<div class="panelBottom">
Main Buttons:
<label class="btn1">1</label>
<label class="btn2">2</label>
<label class="btn3">3</label>
</div>
</div>
<div class="panel" id="item2">
<div class="panelTop">
Dependant Buttons:
<div>
<input type="checkbox">
<div>1.1</div>
<div>1.2</div>
<div>1.3</div>
</div>
<div>
<input type="checkbox">
<div>2.1</div>
<div>2.2</div>
<div>2.3</div>
</div>
<div>
<input type="checkbox">
<div>3.1</div>
<div>3.2</div>
<div>3.3</div>
</div>
</div>
<div class="panelBottom">
Main Buttons:
<label class="btn1">1</label>
<label class="btn2">2</label>
<label class="btn3">3</label>
</div>
</div>
相关文章:
- 我有多个复选框,并希望为其中 2 个制定规则,以便如果我选择 1 个,则无法选择另一个
- 如何发现至少有一个复选框被选中或没有使用javascript
- 我有两个复选框,只有一个可以点击
- 使用复选框有条件地更新 Bing 地图上的标记
- 是否可以让一个复选框包含一个函数
- 我有一个**Rad Combo Box**,其中有两个分隔符.如何仅从分隔符中删除复选框
- 如果有一个子项未选中,则取消选中父项复选框
- MVC中有两个复选框,但每次只允许一个复选框
- Jquery复选框影响一个项目而不是所有项目
- Angularjs如何根据另一个列表中是否存在重复元素来有条件地勾选一个复选框
- 有问题,获得一个复选框值与开关情况
- 我如何在引导中验证至少有一个复选框被标记并将此信息传递给php ?
- 向每个复选框添加一个eventListener
- jquery从另一个复选框控制一个复选复选框
- 根据选中的复选框返回一个字符串
- 函数Javascript的复选框在一个asp:表
- Javascript复选框:使用一个按钮将一个组更改为checked
- jquery复选框有一次变为选中/未选中,如果我再次尝试,它不会'不起作用
- 如何为每个复选框分配一个唯一的id
- 通过单击复选框将一个文本框中的数据复制到另一个文本框中