2016-11-25 158 views
0

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

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

但是我只能得到第一个元素。我怎样才能通过所有参数得到它?

回答

0

您是否尝试使用$.url().param('Mathes');来获得您的Mathes?

0

阿列克谢,试图理解这个概念,如果你想创建多个同名的复选框,比的名字就像是一个数组:

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

你可以尝试这样的事情在你的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); 
     } 
     } 
    }); 
}); 
  • 我已根据“&”首先拆分URL,然后根据=找来
  • 然后我又增加了循环的值与值检查所有的值在url中的复选框
相关问题