0

对于 div 中的动态复选框选择,我可以使用以下代码获取选定的复选框 id,

$(document).on("click", "input[class='CheckFree']",
    function () {
        var allVals = [];
        $('.CheckFree:checked').each(function () {
            allVals.push($(this).attr('id'));
        });
        alert(allVals);
    });

但是现在我想使用这个锚标签而不是复选框,当我点击链接时,它将被添加并获取点击链接的列表
<a class="CheckFree" href="javascript:;" id="cb-{{index}}">Add my Selection</a>

我需要一个我们能做或不能做的可行性。请给我建议。

4

2 回答 2

1
$(document).on("click", ".CheckFree",
    function (e) {
      e.preventDefault();// by this anchor tyag default action not occur
       $(this).addClass("Checkactive");// here we add a class to anchor tag which is clicked
        var allVals = [];
        $('.Checkactive').each(function () {
            allVals.push($(this).attr('id')); // here we push all clicked anhor tag links
        });
        alert(allVals.join(","));// by this all value come with comma seprated
    });
于 2013-11-08T05:25:59.023 回答
1

A possible solution is to add a class whenever a link is selected to indicate that the link is selected, then use the added class as a filter to find out the selected links.

$(document).on('click', '.CheckFree', function () {
    $(this).toggleClass('selected');

    var allVals = $('.CheckFree.selected').map(function () {
        return this.id
    }).get();
    alert(allVals);
})
于 2013-11-08T05:26:53.757 回答