【问题标题】:JQuery: Checkbox won't stay Checked when toggling between buttonsJQuery:在按钮之间切换时复选框不会保持选中状态
【发布时间】:2017-09-21 03:51:27
【问题描述】:

我承认,我仍然是 jquery 的菜鸟,我有一个页面,总共有 12 个按钮/切换按钮(仅显示 3 个作为示例),当单击每个按钮时,都会拉出一个 php 页面,其中列出了一组复选框在高级搜索页面上的 div 中包含产品。由于每个复选框列表之间有很多信息,它会减慢系统速度,因此我必须在单击时创建一个事件,它会在需要时从另一个 php 页面中提取该复选框信息。问题是......当我在按钮之间切换时,复选框不会保持选中状态。我尝试了一个功能,它在检查时设置本地存储项,但似乎不起作用。因此,需要您帮助了解如何在通过每个按钮/切换返回时使复选框保持选中状态。

那么有没有办法在切换时让我的复选框保持选中状态?以及如何(请使用代码示例)

有没有更好的方法可以提高这项工作的效率,从而更快地加载页面?如果是这样,如何? (请使用代码示例)

KITE_PAGE.PHP(球类、呼啦圈等产品也一样)

     <div>
        <input type="checkbox" class="product_chkKite101" name="product_chk" value=" Like Kite 101 " /><label >Like USA Kite</label>
        <input type="checkbox" class="product_chkKite102" name="product_chk" value=" Like Kite 102 " /><label >Like Mexico Kite</label>
        <input type="checkbox" class="product_chkKite103" name="product_chk" value=" Like Kite 103 " /><label >Like Canada Kite</label>
    </div>

NOTLIKE_KITE_PAGE.PHP(对球、呼啦圈等产品同样适用)

     <div>
        <input type="checkbox" class="product_chkKite101" name="product_chk" value=" Not Like Kite 101 " /><label >Not Like USA Kite</label>
        <input type="checkbox" class="product_chkKite102" name="product_chk" value=" Not Like Kite 102 " /><label >Not Like Mexico Kite</label>
        <input type="checkbox" class="product_chkKite103" name="product_chk" value=" Not Like Kite 103 " /><label >Not Like Canada Kite</label>

    </div>

高级搜索页面:

来自高级搜索页面的 HTML

按钮(同样只显示了三个):

    <ul class="category">
        <li><a href="#kite" class="category-btn kite-btn">KITES</a></li>
        <li><a href="#ball" class="category-btn ball-btn">BALLS</a></li>
        <li><a href="#hoop" class="category-btn hoop-btn">HULA HOOPS</a></li>
    </ul>

页面将加载到的 Div:

    <div id="product_container" >
        <div class="kite box" >
            <div class="tx-row" id='kite-in' >

            </div>
            <div class="tx-row"  id='kite-x' >

            </div>
        </div>
        <div class="ball box" >
            <div class="tx-row" id='ball-in' >

            </div>
            <div class="tx-row"  id='ball-x' >

            </div>
        </div>
        <div class="document box" >
            <div class="tx-row" id='hoop-in' >

            </div>
            <div class="tx-row"  id='hoop-x' >

            </div>
        </div>
    </div>

单击按钮时检索信息的JQuery

    $(document).ready(function(){   
        var $content = $('.box');
        function showContent(type) {
            $content.hide().filter('.' + type).show();
        }

        $('.category').on('click', '.kite-btn', function(e) {
            if ($('#kite-in').is(':empty')){                                                                                                
                showContent(e.currentTarget.hash.slice(1));
                e.preventDefault();
            }else {                                                     
                $("#kite-in").load("/wp-content/themes/child/kite_page.php");
                $("#kite-x").load("/wp-content/themes/child/notlike_kite_page.php");                                        
                showContent(e.currentTarget.hash.slice(1));
                e.preventDefault();
            }
        }); 
        $('.category').on('click', '.ball-btn', function(e) {
            if ($('#ball-in').is(':empty')){                                                                                                
                showContent(e.currentTarget.hash.slice(1));
                e.preventDefault();
            }else {                                                     
                $("#ball-in").load("/wp-content/themes/child/ball_page.php");
                $("#ball-x").load("/wp-content/themes/child/notlike_ball_page.php");                                        
                showContent(e.currentTarget.hash.slice(1));
                e.preventDefault();
            }
        });
        $('.category').on('click', '.hoop-btn', function(e) {
            if ($('#hoop-in').is(':empty')){                                                                                                
                showContent(e.currentTarget.hash.slice(1));
                e.preventDefault();
            }else {                                                     
                $("#hoop-in").load("/wp-content/themes/child/hoop_page.php");
                $("#hoop-x").load("/wp-content/themes/child/notlike_hoop_page.php");                                        
                showContent(e.currentTarget.hash.slice(1));
                e.preventDefault();
            }
        });
        $(".box").hide();
    });

对此还有一个附加问题: 当在页面之间检查一个复选框时,有没有办法禁用具有相同类的复选框(例如:class="product_chkKite101")?

【问题讨论】:

  • 这是重现问题的最低代码吗?
  • localstorage 应该适用于您的情况

标签: javascript php jquery ajax checkbox


【解决方案1】:

首先,您可以对所有复选框使用 click 属性并传递当前元素并更好地使用 id,因为它是唯一的,并且对于一个复选框只有一个

 <input id="product_chkKite101" type="checkbox" onclick="checkChecked(this)" class="product_chkKite101" name="product_chk" value=" Not Like Kite 101 " /><label >Not Like USA Kite</label>

现在在 checkChecked 函数中首先创建一个空数组

var checkedCheckbox = [];

function checkChecked(ele){
   var id = ele.id;
   if ($.inArray(id, checkedCheckbox) !== -1)
   {
       var index = checkedCheckbox.indexOf(id);
       if (index > -1) {
          checkedCheckbox.splice(index, 1);
       }
   }else{
       checkedCheckbox.push(id);
   }
   localStorage.setItem("selected", JSON.stringify(selected));

}

现在可以在重新加载页面或点击按钮时选中复选框,您可以运行此代码

var checkCheckedStorage  = [];
$(function() {
  if (localStorage.getItem("selected") != null) {
    checkCheckedStorage = JSON.parse(localStorage.getItem("selected"));
        checkChecked = checkCheckedStorage;
    checkCheckedStorage.forEach(function(ele) {
      console.log(ele);
      $('#' + ele).prop('checked', true);
    });
  }
});

如果在切换按钮时这段代码没有被执行,那么你可以做的是再次在函数中编写代码并从按钮的切换事件中调用它

【讨论】:

  • 尝试你的方法。甚至尝试放入按钮的切换事件,但也许我没有做对。似乎每次我点击按钮时,它都会重新加载页面,导致它删除所有其他复选标记。我什至点击同一个按钮,它会删除选中的复选框。
  • jsfiddle.net/gahc2rqh 在这里我做了类似的事情不一样但相似
  • 我想我找到了问题的根源。这是我的 if 语句检查 id 是否为空。它认为它总是空的,因此总是一遍又一遍地加载页面。现在让这部分工作。
【解决方案2】:

我知道为什么它没有保持检查。每次我再次单击按钮/切换时,它都会重新加载信息。我的 jquery 中的 if 语句是正确的。

更新 jQuery

必须在每个页面的 div 中添加一个 id。然后检查id是否存在。如果不存在,则上传页面。如果存在,则使用当前页面内容。

    $('.category').on('click', '.kite-btn', function(e) {
        if ($('#kitechk').length){                                                                                              
            showContent(e.currentTarget.hash.slice(1));
            e.preventDefault();
        }else {                                                     
            $("#kite-in").load("/wp-content/themes/child/kite_page.php");
            $("#kite-x").load("/wp-content/themes/child/notelike_kite_page.php");                                       
            showContent(e.currentTarget.hash.slice(1));
            e.preventDefault();
            alert('finish')
        }
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-05
    • 1970-01-01
    相关资源
    最近更新 更多