[英]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);
}
}
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.