0

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..

1 Answer 1

1

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(','));.

Sign up to request clarification or add additional context in comments.

2 Comments

Edited Please have a look
@Arun Just add code like my example with different field name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.