[英]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);
}
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.