Javascript onclick复选框将复选框值附加到URL

Javascript onclick复选框将复选框值附加到URL,javascript,jquery,checkbox,onclick,query-string,Javascript,Jquery,Checkbox,Onclick,Query String,我想用jquery而不是内联来实现这一点,但它不起作用,内联很好。我想使用jquery的另一个原因是,如果用户选择了多个复选框,url应该附加任何已经存在的内容+或“第二个复选框值”,如下所示: "http://mysite/sites/dev/contact-us/Pages/LocationSearchTestPage.aspx?s=bcs_locations&k=Office 或医院“ 前面和后面的空间很好。。 我怎样才能做到这一点?有人能帮我吗 Offices<input nam

我想用jquery而不是内联来实现这一点,但它不起作用,内联很好。我想使用jquery的另一个原因是,如果用户选择了多个复选框,url应该附加任何已经存在的内容+或“第二个复选框值”,如下所示: "http://mysite/sites/dev/contact-us/Pages/LocationSearchTestPage.aspx?s=bcs_locations&k=Office 或医院“ 前面和后面的空间很好。。 我怎样才能做到这一点?有人能帮我吗

 Offices<input name="LocType" type="checkbox"  
value="Office" onclick="window.location='http://mysite/sites/dev/contact-us/Pages/LocationSearchTestPage.aspx?s=bcs_locations&k=Office'; return true;"> &#160;
     Hospitals<input name="LocType" type="checkbox"  
value="Hospital" onclick="window.location='http://mysite/sites/dev/contact-us/Pages/LocationSearchTestPage.aspx?s=bcs_locations&k=Hospital'; return true;"> &#160;
     Facilities<input name="LocType" type="checkbox"  
value="Facility" onclick="window.location='http://mysite/sites/dev/contact-us/Pages/LocationSearchTestPage.aspx?s=bcs_locations&k=Facility'; return true;"> 
办公室 ;
医院 家;
设施

绑定到复选框上的更改事件。单击时,读取当前复选框值,然后读取所有其他相关复选框。用自定义查询字符串附加基本url,然后发疯。:)

这没有经过测试,但希望这是一个很好的起点

var baseUrl = 'http://mysite/sites/dev/contact-us/Pages/LocationSearchTestPage.aspx?s=bcs_locations&k=';

$(document).ready(function () {

    // listen to change event (customize selector to your needs)
    $('input[type=checkbox]').change(function (e) {

        e.preventDefault();

        if ($(this).is(':checked')) {

            // read in value
            var queryString = $(this).val();

            // loop through siblings (customize selector to your needs)
            var s = $(this).siblings();
            $.each(s, function () {

                // see if checked
                if ($(this).is(':checked')) {

                    // append value
                    queryString += ' OR ' + $(this).val();
                }
            });

            // jump to url
            window.location = baseUrl + queryString;
        }
    });

});
你可以试试这个

HTML


jQuery
map()
reference-

您已经发布了,有人不厌其烦地回答了,然后有两个人回答了。非常感谢,第二部分不起作用。这不是看是否检查了兄弟姐妹,如果检查了兄弟姐妹,也不是抓取值并附加到URL,或者…你能帮我弄清楚吗?@brian这太棒了!效果很好。不知道您是否可以帮助我,而不是在选中复选框后单击“我想重定向”复选框时自动重定向。你打算怎么做?
<input name="LocType" type="checkbox" value="Office" />
<input name="LocType" type="checkbox" value="Hospital" />
<input name="LocType" type="checkbox" value="Facility" />
var url = "http://mysite/sites/dev/contact-us/Pages/LocationSearchTestPage.aspx?s=bcs_locations";

$('button').click(function(){
   //This will get the array containing values of checked LocType checkboxes
   var checkedLocTypeValues = $('input[name=LocType]:checked').map(function(){
        return this.value;
   });

   //Use Array.join() method to join the array elements by " OR "
   url = url + "&k=" + checkedLocTypeValues.join(" OR ");

   //Now you can use url variable which has all the checked  LocType checkboxes value
}