使用javascript/jQuery从url值开始的自动复选框

nov*_*ick 2 javascript jquery checkboxlist

有没有办法或可以使用javascript/jQuery做到这一点?

基于链接的URL:第一个将检查类型A和类型B,第二个示例将检查类型D.

?type=A&type=C or ?type=D
Run Code Online (Sandbox Code Playgroud)

我目前的表格:

<form name="input" action="" method="post" ><br />
Select Type: <br />
<input type="checkbox" name = "type" value="A" /> A <br />
<input type="checkbox" name = "type" value="B"  /> B <br />
<input type="checkbox" name = "type" value="C"  /> C <br />
<input type="checkbox" name = "type" value="D" /> D <br /> <br />
<input type="submit" value="Submit" />
Run Code Online (Sandbox Code Playgroud)

有任何提示并建议这样做吗?

Eng*_*eer 9

像这样做:

var i = document.location.href.lastIndexOf('?');
var types = document.location.href.substr(i+1).replace(/type=/g,'').split('&');
$('input[name="type"]').prop('checked',function(){
     return $.inArray(this.value,types) !== -1;
});
Run Code Online (Sandbox Code Playgroud)

说明:

考虑网址是' http://sample.com?type=A&type=C'.然后: