mercredi 23 novembre 2016

Jquery if one of the two checkboxes is checked in list.

I have a form which contains two lists of radio checkboxes.

I need a validation check wether one checkbox is checked in either list. To illustrate:

   <form>
    <input type="radio" name="1">
    <input type="radio" name="1">

    <input type="radio" name="2">
    <input type="radio" name="2">

    <button type="submit"></button>
    </form>

so when the form submits, it needs to check wether a checkbox is checked in 'name1' or in 'name2' or in both. If not,it should not let the request through, and give an error message: "This is required". I tried doing this in Jquery with the following code:

$("#submit").on("click",function(){
      if ($("input[name*='name1']:checked") || $("input[name*='name2']:checked")) {
        $("form").submit(function(e){
                e.preventDefault();
            });

        $('#segmentError').toggle();

        if($("#Error").is(':visible')) {
          $("html, body").animate({scrollTop: $("#scrollHere").offset().top});
        }
      }
      return true;
    });

However, now it won't let the request through whatsoever. I'm not that good with JQuery, so I hope one of you can help me with this problem.




Aucun commentaire:

Enregistrer un commentaire