1

In my page I have some checkboxes.I need to pass the checked values through url as query string. here is my checkbox code

 <div class="panel-body">
                <div class="rowElem">
                  <input type="checkbox" name="chbox" id="">
                  <label>Color #1</label>
                </div>
                <div class="rowElem">
                  <input type="checkbox" name="chbox" id="">
                  <label>Color #2</label>
                </div>
                <div class="rowElem">
                  <input type="checkbox" name="chbox" id="">
                  <label>Color #3</label>
                </div>
              </div>

my initial url is like www.test.com if I check the color #3 then the url will look like www.test.com/?color=color #3

How to done this using ajax.???

Arun
  • 1,402
  • 10
  • 32
  • 59

3 Answers3

2

Try this. On each checkbox change post to the server and html5 pushState the URL with the data.

$('input[type="checkbox"]').on('change', function(e){
    var data = $('input[type="checkbox"]').serialize(),
        loc = $('<a>', {href:window.location})[0];
    $.post('/ajax-post-url/', data);
    if(history.pushState){
        history.pushState(null, null, loc.pathname+'?'+data);
    }
});

Updated to work with same named inputs by adding the index to the end.

$('input[type="checkbox"]').on('change', function(e){
    var data = [],
        loc = $('<a>', {href:window.location})[0];
    $('input[type="checkbox"]').each(function(i){
    if(this.checked){
        data.push(this.name+i+'='+this.value);
    }
    });
    data = data.join('&');

    $.post('/ajax-post-url/', data);
    if(history.pushState){
        history.pushState(null, null, loc.pathname+'?'+data);
    }
});

Updated to work as comma separated list. key=a,b,c,d,e.

$('input[type="checkbox"]').on('change', function(e){
    var data = {},
        fdata = [],
        loc = $('<a>', {href:window.location})[0];
    $('input[type="checkbox"]').each(function(i){
        if(this.checked){
            if(!data.hasOwnProperty(this.name)){
                data[this.name] = [];
            }
            data[this.name].push(this.value);
        }
    });
    $.each(data, function(k, v){
        fdata[k] = [v.join(',')];
    });
    fdata = fdata.join('&');

    $.post('/ajax-post-url/', fdata);
    if(history.pushState){
        history.pushState(null, null, loc.pathname+'?'+fdata);
    }
});
Adam Merrifield
  • 10,307
  • 4
  • 41
  • 58
1

Try this:

Html:

<div class="panel-body">
   <div class="rowElem">
      <input type="checkbox" name="chbox" id="" value="Color #1"> <!-- add value field-->
       <label>Color #1</label>
   </div>
   <div class="rowElem">
      <input type="checkbox" name="chbox" id="" value="Color #2"> <!-- add value field-->
       <label>Color #2</label>
   </div>
   <div class="rowElem">
     <input type="checkbox" name="chbox" id="" value="Color #3"> <!--add value field-->
       <label>Color #3</label>
    </div>
</div>

Jquery:

$('input[name="chbox"]:checked').serialize();

Demo

Joke_Sense10
  • 5,341
  • 2
  • 18
  • 22
1

This will find all the checked checkboxes on change event and then finds the associated label elements text

var $checks = $('input[name="chbox"]').change(function () {
    var checked = $checks.filter(':checked').map(function () {
        return $.trim($(this).next().text());
    }).get().join(',')
    var url = 'www.test.com/?color=' + checked;
    //do you ajax
})
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531