使用复选框将内容附加到列表

发布于 2024-09-25 23:53:01 字数 2786 浏览 0 评论 0原文

感谢几位 Stack Overflow 参与者的宝贵帮助,我非常接近期望的结果……对于感兴趣的各方,这里有一个回顾和更新:

我有一系列产品模块,每个模块都包含(除其他外)产品的名称和“比较”复选框。我的目标是在选中复选框时在页面的另一个区域生成选定产品的列表。因为必须有一个函数来检查至少两个和任意最大值 - 要么作为复选框 .click 函数的一部分(根据布莱恩的版本,再次感谢您的宝贵帮助),要么在提交上(我是倾向)。无论哪种情况,列表中的项目数量显然都被错误计算了;选中零个或一个复选框后,我正确地收到了物品不足的警报;但是,如果我取消选中并重新检查几次,我的代码会报告列表中的项目数量不正确。

为了简洁起见(!),我将放弃发布这两个版本,因为我在这两个版本中都遇到了类似的奇怪情况。这是我最新的鹅卵石 - 非常感谢您的任何见解。

<div class="product-module">

<div class="product-pic">
    <div class="checkbox-wrapper">
        <label for="compare1">
            <input type="checkbox" class="checkbox" name="compare" id="compare1" />
            Compare
        </label>
    </div>
</div>

<div class="product-info">
    <p>
        <a href="#" title="#"><span class="product-name">Product Name here</span></a><br />
    </p>
</div>

<div class="compare">
<ul>
</ul>
<p class="error" style="display: none;">ACK! You've selected more than 4 products to compare.</p>
<p class="compare-button"><button type="submit">Compare</button></p>
<p class="clear-selections"><a class="button" id="clear-selections" href="#">Clear Selections</a></p>

<script type="text/javascript">

$(function(){
    $('input[type="checkbox"]').attr('checked', false);
});

$(function(){
    $('input[type="checkbox"]').click(function(){
        var title = $(this).closest('.product-module').find('.product-name').html();

        // as user checks the checkbox, add the item to the ul
        if($(this).attr('checked')){

        var html = '<li><a href="'+title+'">' + title + '</a></li>';

        $('.compare ul').append(html);
        } else {     
        // if user is un-checking the checkbox, remove the item from the ul
        // orig: $('li[title="' + title + '"]').remove();
        $('li a[href="' + title + '"]').remove();
        }
    });
});


$(function(){
    $('.clear-selections').click(function(){
        $('.compare ul').empty();
        $('input[type="checkbox"]').attr('checked', false);
    })
});


$(function(){
    $('.compare button').click(function(){
        minRequests = 2;
        maxRequests = 4;
        requested = $('.compare ul li').size();    // go figure: why not .length()?

        if(requested < 2)    {
        alert ('Compare ' + requested + ' products?');

        } else if((requested >= 2) && (requested <= 5 ))    {
        alert ('There are ' + requested + ' products to compare');

        } else {
        alert (requested + ' is too many');
        }
    });
});

Thanks to the invaluable assistance of several Stack Overflow participants, I'm very close to the desired results ... here's a recap and update, for interested parties:

I have a series of product modules, each of which contains (among other things) the product's name and a 'compare' checkbox. My objective is to generate a list of selected products in another area of the page when the checkboxes are checked. As there must be a function to check for a minimum of two, and some arbitrary maximum - either as part of the checkbox .click function (per Brian's version, thanks again for your invaluable help), or on the submit (which I'm leaning towards). In either case, the number of items in the list is apparently being mis-counted; w/zero or one checkbox checked, I correctly get an alert of insufficient items; however, if I uncheck and recheck a few times, my code is reporting the incorrect number of items in the list.

In the interest of brevity (!), I'll forego posting both versions, since I'm experiencing similar oddity in both. Here's my latest cobbling - many thanks in advance for any insight.

<div class="product-module">

<div class="product-pic">
    <div class="checkbox-wrapper">
        <label for="compare1">
            <input type="checkbox" class="checkbox" name="compare" id="compare1" />
            Compare
        </label>
    </div>
</div>

<div class="product-info">
    <p>
        <a href="#" title="#"><span class="product-name">Product Name here</span></a><br />
    </p>
</div>

<div class="compare">
<ul>
</ul>
<p class="error" style="display: none;">ACK! You've selected more than 4 products to compare.</p>
<p class="compare-button"><button type="submit">Compare</button></p>
<p class="clear-selections"><a class="button" id="clear-selections" href="#">Clear Selections</a></p>

<script type="text/javascript">

$(function(){
    $('input[type="checkbox"]').attr('checked', false);
});

$(function(){
    $('input[type="checkbox"]').click(function(){
        var title = $(this).closest('.product-module').find('.product-name').html();

        // as user checks the checkbox, add the item to the ul
        if($(this).attr('checked')){

        var html = '<li><a href="'+title+'">' + title + '</a></li>';

        $('.compare ul').append(html);
        } else {     
        // if user is un-checking the checkbox, remove the item from the ul
        // orig: $('li[title="' + title + '"]').remove();
        $('li a[href="' + title + '"]').remove();
        }
    });
});


$(function(){
    $('.clear-selections').click(function(){
        $('.compare ul').empty();
        $('input[type="checkbox"]').attr('checked', false);
    })
});


$(function(){
    $('.compare button').click(function(){
        minRequests = 2;
        maxRequests = 4;
        requested = $('.compare ul li').size();    // go figure: why not .length()?

        if(requested < 2)    {
        alert ('Compare ' + requested + ' products?');

        } else if((requested >= 2) && (requested <= 5 ))    {
        alert ('There are ' + requested + ' products to compare');

        } else {
        alert (requested + ' is too many');
        }
    });
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无悔心 2024-10-02 23:53:01

这是我采取的方法: http://jsfiddle.net/ek2zh/ 我没有使用克隆,因为您要粘贴的节点是一个跨度,并且您想将其克隆到一个 li 中。不确定克隆是否会根据需要自动调整节点。

Here's the approach I take: http://jsfiddle.net/ek2zh/ I didn't use clone because the node you want to paste is a span and you want to clone it into an li. Not sure clone will automatically adjust the node as desired.

回眸一遍 2024-10-02 23:53:01

我猜您的代码正在克隆 .product-name 克隆并再次克隆它。尝试使用更具体的路径来定位克隆的元素,而不是 find 方法。

I'm guessing your code is cloning the .product-name clone and cloning it again. Try using a more specific path to target the element your cloning, instead of the find method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文