Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I tried some jquery to append my check box checked values to the url as query string the code is working nice but when I check more than one checkbox the url will like this.....

Localhost:355/searchdatabase/?manufacturer=LG&manufacturer=Samsung&manufacturer=Test

But I need the url Like

Localhost:355/searchdatabase/?manufacturer=LG,Samsung,Test

here is my code

$(document).ready(function () {
    $('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 + '=' + this.value);
            }
        });
        data = data.join('&');

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

My checkbox list groups code here

          <div class="rowElem">
          <input type="checkbox" name="manufacturer" id="">
          <label>LG</label>
        </div>
        <div class="rowElem">
          <input type="checkbox" name="manufacturer" id="">
          <label>Samsung</label>
        </div>
        <div class="rowElem">
          <input type="checkbox" name="manufacturer" id="">
          <label>Test</label>
        </div>




         <div class="rowElem">
          <input type="checkbox" name="material" id="">
          <label>iron</label>
        </div>
        <div class="rowElem">
          <input type="checkbox" name="material" id="">
          <label>steel</label>
        </div>
        <div class="rowElem">
          <input type="checkbox" name="material" id="">
          <label>copper</label>
        </div>

I need Manufacturer as a group and material as another..

share|improve this question
add comment

1 Answer

You can add all manufacturers to separated array and then add as one field.

var manufacturers = [];
if (this.checked) {
    if (this.name === 'manufacturers') {
        manufacturers.push(this.value);
    } else {
        data.push(this.name + '=' + this.value);
    }
}

And before data = data.join('&'); add data.push('manufacturers=' + manufacturers.join(','));.

share|improve this answer
 
Edited Please have a look –  Arun Dec 5 '13 at 12:40
 
@Arun Just add code like my example with different field name. –  Elon Than Dec 5 '13 at 12:46
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.