Anjana Sharma
Anjana Sharma

Reputation: 4745

checkbox onclick pass chechbox value as querystring to URL

I have a couple of chechboxes and on click of anyone of them, should trigger an event that captures the checkbox value and pass to the URL as querystring.

querystring should be like this: mysite.com/result.aspx?k=Hospital OR Office OR Facility if all three checkboxes are checked one after another..so the first time Hospital is checked, it will redirect to mysite.com/result.aspx?k=Hospital and second time Offices checkbox is checked, it will add to the existing URL the value like mysite.com/result.aspx?k=Hospital OR Office and so on...

<div class="MyOptions">
 Offices<input name="LocType" type="checkbox"  value="Office"/> &#160;
 Hospitals<input name="LocType" type="checkbox"   value="Hospital"/>&#160; 
 Out-Patient Centers<input name="LocType" type="checkbox"  value="Out-Patient"/>&#160; 
 Facilities<input name="LocType" type="checkbox"  value="Facility"/>
</div>

** This is not working..can anyone guide me. I am not very good with Jquery..

$(document).ready(function() {  
    var url = 'http://mysite.com/results.aspx';  
    $(".MyOptions input").click(function() { 

        var urlValues = window.location.href.split("k=(")[1]; 
        urlValues = urlValues.substring(0,urlValues .length - 1); 

        var checkboxValues = $("input[name=LocType]:checked").map(function() {return "\"" + this.value + "\"";}).get().join(" OR ") 

        if (urlValues.length > 0) 
            urlValues += " OR " + checkboxValues; 


     window.location.href = "http://mysite.aspx/result.aspx?k=" + urlValues;

        });

**

Upvotes: 1

Views: 2007

Answers (1)

Malk
Malk

Reputation: 11983

This is not the correct way to send checkboxes to a server. You have a custom process parsing the query string on the other end? Anyway, change your map to use the jquery $(this):

var checkboxValues = $("input[name=LocType]:checked").map(function() {return "\"" + $(this).val() + "\"";}).get().join(" OR ")

Upvotes: 1

Related Questions