根据URL查询字符串中的复选框值在页面刷新后设置复选框

我有一组复选框:

HOSPITALS    PHYSICIAN OFFICES   EMERGENCY CENTERS   OUT-PATIENT CENTERS   FACILITIES 

我已经抓住他们的值如下并传递给url,在页面重新加载传递的查询字符串后,我想检查传递的值是什么复选框,并将这些复选框设置为checked = true。

 var url = "http://mysite.com/contact-us/Pages/LocationSearch.aspx?s=bcs_locations&k="; var checkboxValues = $("input[name=LocType]:checked").map(function() { return ' '+'bcslocationtype:'+"\"" + $(this).val() + "\"";}).get().join(" OR "); window.location= url + checkboxValues; 

当选中Hospitals and Emergency复选框并点击搜索时,URL如下所示: http ://mysite.com/contact-us/Pages/LocationSearch.aspx?s = bcs_locations&k = bcslocationtype:“Hospital”或bcslocationtype:“Emergency”

现在为什么事情不起作用,我没有看到任何错误?它的insde document.ready(){}

 //Keep the selected chkboxes checked on page redirect var value = window.location.href.match(/[?&]k=([^&#]+)/) || []; if (value.length == 2) { $('input[name=LocType]').each(function (index) { if(value[1].indexOf($(this).val())>=0) this.prop("checked", true); }); } 

这是您的代码的更新

  $(document).ready(function(){ var value = window.location.href.match(/[?&]k=([^&#]+)/) || []; var actualValue = value[1].split(":") console.log(actualValue[1]); $('input[name=LocType]').each(function (index) { if(actualValue[1] === $(this).val()){ //or use if(actualValue[1].indexOf($(this).val())>=0){ $(this).prop("checked", true); } }); }); 

还要注意,为了访问像’prop’这样的jquery相关方法,你需要在’$’中包含’this’。 所以我将你的代码从’this’更新为’$(this)’。 另外,我们的目的是使用’bcslocationtype:“医院”’作为价值。 为什么需要’bcslocationtype’作为变量k的值。 你不能只是追加k =医院等吗?

此外,您可以使用aspx执行相同的操作。 我不确定aspx代码,但在Coldfusion中我会做类似下面的事情:假设’k’是url变量名

  CHECKED />HOSPITALS   

输入标记中要仔细阅读的代码是CHECKED 。 这意味着,如果我在URL范围内定义并设置了名为“k”的变量,并且该值为hospital,则将其选中。 将其转换为您的asp代码。