简体   繁体   English

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

[英]State checkbox after page reload

I need to save checkbox status after page reload. 页面重新加载后,我需要保存复选框状态。 My Query string: 我的查询字符串:

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

How can I get this data? 我如何获得这些数据? I wrote some code: 我写了一些代码:

$(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');
        }
    });
});

But I can get only first element. 但是我只能得到第一个要素。 How can I get it all by all parameters? 如何获得所有参数的全部信息?

Have you try to use $.url().param('Mathes'); 您是否尝试过使用$.url().param('Mathes'); to get your Mathes? 得到你的数学?

Alexey, try to understand the concept, if you want to create multiple checkbox with same name, than the name is an array like: 阿列克谢,尝试理解这个概念,如果要创建多个具有相同名称的复选框,则名称是一个数组,如:

<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" />

and get its value like: 并获得其值,例如:

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

You could try out something like this in your js code, 您可以在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);
        }
        }
    });
});
  • I have split the url first based on "&" and then based on = and got the values 我首先根据“&”分割了网址,然后根据=分割了网址,并获得了值
  • Then i have added another for loop to check all the values in the url with the values in the checkboxes 然后我添加了另一个for循环来检查url中的所有值以及复选框中的值

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

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