我需要在页面重新加载后保存复选框状态。我的查询字符串:
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 :(得分:0)
您是否尝试使用$.url().param('Mathes');
获取Mathes?
答案 1 :(得分:0)
Alexey,尝试理解这个概念,如果你想创建多个具有相同名称的复选框,那么名称就像是一个数组:
<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());
}
});
答案 2 :(得分: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);
}
}
});
});