【问题标题】:jquery uncheck checkbox with a linkjquery取消选中带有链接的复选框
【发布时间】:2013-07-25 03:09:30
【问题描述】:

请指教,我想创建标签。我在一个 div 中有一个输入字段,在另一个 div 中有多个复选框。我想添加标签单击复选框。标签有一个关闭按钮,可以删除该标签,同时取消选中特定的复选框。

例如,我有汽车、时尚、医疗保健、教育等复选框。在输入框中输入添加标签并通过按回车检查复选框。

$(document).ready(function() {
var $list = $("#itemList");

$(".chkbox").change(function() {
    var a = this.value;
    if (this.checked) {
        $list.append('<li><a href="#">' + a + '</a></li>');
    }
    else {
        $("#itemList li:contains('"+a+"')").remove();
    }
})
$('div a').live('click', function(ev){
    alert("hi");
});


<div id="tab1" class="tab-pane">
    <input type="checkbox" class="chkbox" value="101">  This is 101
    <input type="checkbox" class="chkbox" value="102">  This is 102
    <input type="checkbox" class="chkbox" value="103">  This is 103
    <input type="checkbox" class="chkbox" value="104">  This is 104
</div>
<div id="items">
    <ul id="itemList">
    </ul>
</div>

请提供帮助或建议。

【问题讨论】:

标签: jquery css checkbox hyperlink


【解决方案1】:

早上

试试这个

var $list = $("#itemList");

$(".chkbox").change(function() {
    var a = this.value;
    if (this.checked) {
        $list.append('<li><a href="#">' + a + '</a><button class="closebutton" value="'+a+'">X</button></li>');
    }
    else {
        $("#itemList li:contains('"+a+"')").remove();
    }
})
$(document).on('click','div a',function(ev){
   alert($(this).text());
});
$(document).on('click','.closebutton',function(){
   var b = this.value;
   $("#itemList li:contains('"+b+"')").remove();
    $(".chkbox[value="+b+"]").removeAttr('checked');
});

这将创建一个也可以单击的关闭按钮..

http://jsfiddle.net/jDBsw/3/

【讨论】:

    【解决方案2】:

    问题可能出在您的 .change 上。试试

    $(document).on('click','.chkbox', function() {
        var a = $(this).val();
        // same code as above
    });
    

    在最新版本的 JQuery 中也不推荐使用 .live。不要使用它。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多