选中复选框时取消绑定 link
Unbind link when checkbox are checked
我有很多复选框和link。如果复选框被选中,我想激活 links
$("a[id^='link-'], a.disabled").unbind("click");
$('#alljc').on("change", ":checkbox", function () {
if (this.checked) {
$('#link-'+this.id).bind('click');
} else {
$('#link-'+this.id).unbind('click');
}
});
Html 示例:
<div id="alljc">
<form class="text-center" action="#">
<input type="checkbox" id="6" />
<a id="link-6" href="i-aggre.php?id=6">i m ok</a>
</form>
...
</div>
unbind
用于取消绑定分配的点击处理程序,它不会对本机功能执行任何操作。最好的办法是切换 href
属性(为方便起见,将 href
存储在 data-*
属性中)
<a id="link-6" data-target="i-aggre.php?id=6" href="i-aggre.php?id=6">i m ok</a>
和 JS:
$('#alljc').on("change", ":checkbox", function () {
if (this.checked) {
var href = $(this).data("target");
$('#link-'+this.id).attr('href', href);
} else {
$('#link-'+this.id).attr('href', '');
}
});
正如我在上面的评论中所述,我认为您不能或不应该采用 .bind()/.unbind()(或 .on()/.off())路线。而是尝试:
$('#alljc a').click(function(e) {
if ($(this).prev('input').is(':not(:checked)')) e.preventDefault()
})
尝试使用 preventDefault() 和 off() 检查我的 DEMO Fiddle
$("a[id^='link-'], a.disabled").on('click',function(e){
e.preventDefault();
});
$('#alljc').on("change", ":checkbox", function(){
var actId=$(this).attr('id');
if ($(this).is(':checked')) {
$('a#link-'+actId).off('click');
} else {
$('a#link-'+actId).on('click',function(e){
e.preventDefault();
})
}
});
我有很多复选框和link。如果复选框被选中,我想激活 links
$("a[id^='link-'], a.disabled").unbind("click");
$('#alljc').on("change", ":checkbox", function () {
if (this.checked) {
$('#link-'+this.id).bind('click');
} else {
$('#link-'+this.id).unbind('click');
}
});
Html 示例:
<div id="alljc">
<form class="text-center" action="#">
<input type="checkbox" id="6" />
<a id="link-6" href="i-aggre.php?id=6">i m ok</a>
</form>
...
</div>
unbind
用于取消绑定分配的点击处理程序,它不会对本机功能执行任何操作。最好的办法是切换 href
属性(为方便起见,将 href
存储在 data-*
属性中)
<a id="link-6" data-target="i-aggre.php?id=6" href="i-aggre.php?id=6">i m ok</a>
和 JS:
$('#alljc').on("change", ":checkbox", function () {
if (this.checked) {
var href = $(this).data("target");
$('#link-'+this.id).attr('href', href);
} else {
$('#link-'+this.id).attr('href', '');
}
});
正如我在上面的评论中所述,我认为您不能或不应该采用 .bind()/.unbind()(或 .on()/.off())路线。而是尝试:
$('#alljc a').click(function(e) {
if ($(this).prev('input').is(':not(:checked)')) e.preventDefault()
})
尝试使用 preventDefault() 和 off() 检查我的 DEMO Fiddle
$("a[id^='link-'], a.disabled").on('click',function(e){
e.preventDefault();
});
$('#alljc').on("change", ":checkbox", function(){
var actId=$(this).attr('id');
if ($(this).is(':checked')) {
$('a#link-'+actId).off('click');
} else {
$('a#link-'+actId).on('click',function(e){
e.preventDefault();
})
}
});