簡體   English   中英

頁面重新加載后的狀態復選框

[英]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);
        }
        }
    });
});
  • 我首先根據“&”分割了網址,然后根據=分割了網址,並獲得了值
  • 然后我添加了另一個for循環來檢查url中的所有值以及復選框中的值

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM