2016-11-25 65 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中的複選框
相關問題