0

I have two categories list Gender and Brand. First category

<ul>
  <li><input type="checkbox" id="checkboxa" class="chkbox" value="url.php?gender=children" name="gender"> Kid</li>
  <li><input type="checkbox" id="checkboxb" class="chkbox" value="url.php?gender=male" name="gender"> Male</li>
<li><input type="checkbox" id="checkboxc" class="chkbox" value="url.php?gender=female" name="gender"> Female</li>
</ul>

Second Category

<ul>
  <li><input type="checkbox" id="checkbox1" class="chkbox" value="url.php?barnd=brand1" name="brand"> Brand1</li>
  <li><input type="checkbox" id="checkbox2" class="chkbox" value="url.php?barnd=brand2" name="brand"> Brand2</li>
  <li><input type="checkbox" id="checkbox3" class="chkbox" value="url.php?barnd=brand3" name="brand"> Brand3</li>
  <li><input type="checkbox" id="checkbox4" class="chkbox" value="url.php?barnd=brand4" name="brand"> Brand4</li>
</ul>

This is my js:

<script>
$(document).ready(function() {
$('.chkbox').on('change', function() {
if ($(this).is(':checked')) {
  var url = $(this).val();
  console.log(url);

  window.location = url;
}
});
});
</script>

Right now my page is reloading when one item is checked. I want to make the page reload only when one item of each category is checked. Plz help... Thanks in advance.....

PS: url should be like url.php?brand=$brand&&gender=$gender $:-checked

3 Answers 3

2

Change your if condition to

if($("input[name='gender']:checked").length && $("input[name='brand']:checked").length) {
  var url = $(this).val();    
  window.location = url;
}

$('.chkbox').on('change', function() {
  var gender = $("input[name='gender']:checked"),
    brand = $("input[name='brand']:checked");
  if (gender.length && brand.length) {
    var url = "url.php?brand=" + brand.val() + "&&gender=" + gender.val();
    console.log(url);
    //window.location = url;
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li>
    <input type="checkbox" id="checkboxa" class="chkbox" value="children" name="gender"> Kid</li>
  <li>
    <input type="checkbox" id="checkboxb" class="chkbox" value="male" name="gender"> Male</li>
  <li>
    <input type="checkbox" id="checkboxc" class="chkbox" value="female" name="gender"> Female</li>
</ul>
<ul>
  <li>
    <input type="checkbox" id="checkbox1" class="chkbox" value="brand1" name="brand"> Brand1</li>
  <li>
    <input type="checkbox" id="checkbox2" class="chkbox" value="brand2" name="brand"> Brand2</li>
  <li>
    <input type="checkbox" id="checkbox3" class="chkbox" value="brand3" name="brand"> Brand3</li>
  <li>
    <input type="checkbox" id="checkbox4" class="chkbox" value="brand4" name="brand"> Brand4</li>
</ul>

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

4 Comments

@lazyme114 It works. I've added a snippet. Are there any errors in your browser console.
My mistake it started working... But I needed url to be something like url.php?brand=brand&&gender=gender
@lazyme114 I've updated the snippet. Modified the values of input and commented the redirect part.
In case of multiple options selected it would choose the value of the first input checked. I guess you only want one option to be checked , in that case you should go for input type radio.
2

Here is the snippet.

$(document).ready(function() {
  $('#fcat .chkbox').on('change', function() {
    if ($(this).is(':checked')) {
      var url = $(this).val();
      if ($(this).is(':checked')) {
        if ($('.cat').is(':checked')) {
          alert(url);
          window.location = url;
        }else{
          alert('Select gender!');
        }
      }
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li><input type="checkbox" id="checkboxa" class="cat" value="url.php?gender=children" name="gender"> Kid</li>
  <li><input type="checkbox" id="checkboxb" class="cat" value="url.php?gender=male" name="gender"> Male</li>
<li><input type="checkbox" id="checkboxc" class="cat" value="url.php?gender=female" name="gender"> Female</li>
</ul>

<ul id="fcat">
  <li><input type="checkbox" id="checkbox1" class="chkbox" value="url.php?barnd=brand1" name="brand"> Brand1</li>
  <li><input type="checkbox" id="checkbox2" class="chkbox" value="url.php?barnd=brand2" name="brand"> Brand2</li>
  <li><input type="checkbox" id="checkbox3" class="chkbox" value="url.php?barnd=brand3" name="brand"> Brand3</li>
  <li><input type="checkbox" id="checkbox4" class="chkbox" value="url.php?barnd=brand4" name="brand"> Brand4</li>
</ul>

If you check gender checkbox and select brand it will reload your brand location else return alert to select gender first. Hope this help you!

Greetings!

Comments

1
<ul>
  <li><input type="checkbox" id="checkboxa" class="chkbox chkType1" value="url.php?gender=children" name="gender"> Kid</li>
  <li><input type="checkbox" id="checkboxb" class="chkbox chkType1" value="url.php?gender=male" name="gender"> Male</li>
<li><input type="checkbox" id="checkboxc" class="chkbox chkType1" value="url.php?gender=female" name="gender"> Female</li>
</ul>
<ul>
  <li><input type="checkbox" id="checkbox1" class="chkbox chkType2" value="url.php?barnd=brand1" name="brand"> Brand1</li>
  <li><input type="checkbox" id="checkbox2" class="chkbox chkType2" value="url.php?barnd=brand2" name="brand"> Brand2</li>
  <li><input type="checkbox" id="checkbox3" class="chkbox chkType2" value="url.php?barnd=brand3" name="brand"> Brand3</li>
  <li><input type="checkbox" id="checkbox4" class="chkbox chkType2" value="url.php?barnd=brand4" name="brand"> Brand4</li>
</ul>

And the javascript:

    <script>
    $(document).ready(function() {
    $('.chkbox').on('change', function() {
    if ($(this).is(':checked')) {

    if(($('.chkType1').length > 0 && $('.chkType2').length > 0))
     {
        var url = $(this).val();

url += '?brand='+$($('.chkType1')[0]).val()+'&&gender=' + $($('.chkType2')[0]).val();

        console.log(url);

      window.location = url;
    }
    }
    });
    });
    </script>

Comments

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.