繁体   English   中英

页面重新加载后的状态复选框

[英]State checkbox after page reload

页面重新加载后,我需要保存复选框状态。 我的查询字符串:

Mathes=1&Mathes=2&Mathes=3&Rols=2&Users=1&.......

我如何获得这些数据? 我写了一些代码:

$(document).ready(function () {
    var value = window.location.href.match(/[?&]Matches=([^&#]+)/);
    $('input[name=Mathes]').each(function (index) {
        if (value[1] === $(this).val()) {
            $(this).attr('checked', 'checked');
        }
    });
});

但是我只能得到第一个要素。 如何获得所有参数的全部信息?

您是否尝试过使用$.url().param('Mathes'); 得到你的数学?

阿列克谢,尝试理解这个概念,如果要创建多个具有相同名称的复选框,则名称是一个数组,如:

<input type="checkbox" name="category[]" class="category" value="1" />
<input type="checkbox" name="category[]" class="category" value="2" />
<input type="checkbox" name="category[]" class="category" value="3" />
<input type="checkbox" name="category[]" class="category" value="4" />
<input type="checkbox" name="category[]" class="category" value="5" />

并获得其值,例如:

var category= [];
$('input[class="category"]').each(function(){
    if($(this).is(':checked'))
    {
        category.push($(this).val());
    }
});

您可以在js代码中尝试类似的方法,

$(document).ready(function () {
    var value = $(location).attr('href').split("?").pop();
  console.log("url"+$(location).attr('href'))
    var strparams=value.split("&");
    for(var i in strparams){
        strparams[i]=strparams[i].substring(strparams[i].indexOf("=")+1,strparams[i].length);
    }

    console.log(""+strparams);
    $('input[name=mathes]').each(function (index) {
        for(var j=0;j<strparams.length;j++){
        if (strparams[j] === $(this).val()) {
            $(this).attr('checked',true);
        }
        }
    });
});
  • 我首先根据“&”分割了网址,然后根据=分割了网址,并获得了值
  • 然后我添加了另一个for循环来检查url中的所有值以及复选框中的值

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM