如果存在img元素,请禁用复选框
Disable check box if there is an img element
如果有一个具有数据属性的img元素(在checkbox元素之前)并且没有选中复选框,我需要禁用复选框并在复选框文本中换行。
<div class="classContainer">
<label>
<img data-disable-checkbox="true">
<input type="checkbox" checked="checked" />
This checkbox value
</label>
<label>
<input type="checkbox" />
This checkbox value
</label>
<label>
<img data-disable-checkbox="true">
<input type="checkbox" />
This checkbox value
</label>
</div>
var img = $("img[data-disable-checkbox='true']");
if (!(img.next("input").is(":checked"))){
img.next().prop("disabled", true);
img.parent().contents().filter(function(){
return this.nodeType == 3 && $.trim($(this).text());
}).wrap($('<span />', {
style: 'text-decoration: line-through'
}));
}
我试过了http://jsfiddle.net/yXVZM/9/
如何添加上面解释的所需行为?
这样做:
$(".test").each( function() {
$this = $(this);
if($this.find("img").length) {
$this.find("input:checked").prop("disabled", "true");
}
});
演示:http://jsfiddle.net/yXVZM/14/
编辑:显然我看错了,认为是检查过的,而不是没有检查过,没有抓住打击-为此:
http://jsfiddle.net/yXVZM/19/
$(".test").each( function() {
$this = $(this);
if($this.find("img").length) {
$notChecked = $this.find("input:checkbox").not(":checked");
if($notChecked.length) {
$notChecked.prop("disabled", "true");
$this.attr("style","text-decoration: line-through");
}
}
});
这是基于这样一个事实,即你有小提琴中每个标签的类。如果你的代码中没有类,你可以做类似的事情。。。或者只是添加一个类。
或者使用这样的选择器来代替$(".classContainer").children().each(....
问题是,您没有对每个图像进行迭代,而是只在第一个图像上运行代码。
我在下面添加了一个.each()
来循环浏览集合中的每个图像:
Fiddle
var img = $("img[data-disable-checkbox='true']");
img.each(function(index, val){
if (!($(this).next("input").is(":checked"))){
$(this).next().prop("disabled", true);
$(this).parent().contents().filter(function(){
return this.nodeType == 3 && $.trim($(this).text());
}).wrap($('<span />', {
style: 'text-decoration: line-through'
}));
}
});
我为此添加了标签和CSS类,怎么样:
JS:
$("input[type='checkbox']").each(function() {
var image = $(this).prev("img[data-disable-checkbox]").length;
var isChecked = this.checked;
if (image && !isChecked) {
$(this).prop("disabled", true);
$(this).next("label").addClass("cbDisabled");
}
});
CSS:
.cbDisabled {
text-decoration:line-through;
}
演示:http://jsfiddle.net/yXVZM/16/
$("img[data-disable-checkbox='true']").each(function() {
var n = $(this).next('input[type=checkbox]').not(':checked').prop('disabled', true).prop('nextSibling');
$(n).wrap('<span class="strike"/>');
});
http://jsfiddle.net/gwJYN/
替换:
<label>
<img data-disable-checkbox="true">
<input type="checkbox" />
This checkbox value
</label>
收件人:
<label class="test">
<img data-disable-checkbox="true">
<input type="checkbox" class="chk" /> <span>
This checkbox value
</span>
</label>
JS:
$(document).ready(function () {
$('.test').each(function () {
if ($(this).find('img').length > 0) {
if ($(this).find('.chk').not(":checked")) {
$(this).find('.chk').prop("disabled", true);
$(this).find('.chk').next('span').css('text-decoration', 'line-through');
}
}
});
});
JSFIDDLE演示
相关文章:
- 如何在angularJS中编辑时,如果DB中的值为true,则设置复选框,如果值为false,则取消选中复选框
- 正在验证8个真/假复选框或复选框中的2个
- 为复选框javascript指定两个值
- 如何在单击复选框后调用控制器方法
- 从复选框和Selects-KnockoutJS中获取值的总和
- 选中多个具有相同名称的复选框
- 使用Jquery选择或取消选择ListView中的所有复选框
- Jquery表单验证插件-如果选中复选框,如何在提交时执行某些操作
- 在运行时添加复选框并将其保存在 HTML 文件代码中
- 选中复选框(如果 json 对象中存在)
- 如何检查是否存在多个同名复选框或仅存在一个此类复选框
- 比较两个数组,如果 Array2 中存在值,则选中复选框
- Spring复选框选中的属性不存在
- 如果存在img元素,请禁用复选框
- 如何检查复选框是否存在
- Angularjs如何根据另一个列表中是否存在重复元素来有条件地勾选一个复选框
- 隐藏复选框,但保留它的存在
- jQuery选择复选框时,文本存在,隐藏未选中的行从打印
- 复选框和
- 如果不在iMacro中刷新,请选中复选框(如果存在)