0

Although the following function has a little problem. But I can not find. Every time either checkbox is checked or not but form was not submitted!!

<input type="checkbox" name="chk_user[]" value="1" class="chk_delete" id="1"  />
<input type="checkbox" name="chk_user[]" value="2" class="chk_delete" id="2"  />
<input type="checkbox" name="chk_user[]" value="3" class="chk_delete" id="3"  />
<input type="checkbox" name="chk_user[]" value="4" class="chk_delete" id="4"  />
<script>
    $("form").submit(function() {
        $('.chk_delete').each(function(){
            if($(this).is(':checked')){            
                return true;

            }
        });
        alert("No entry was selected!");
        return false;
    });
</script>

Can anybody locate the problem?

The following is working, but I don't understand why. Any good logic?

$("form").submit(function(e) { 
    if(!$('input[type=checkbox]:checked').length) { 
        e.preventDefault(); 
        alert("No entry was selected!"); } 
     return true; });
9
  • 1
    Why is your form not wrapped in a <form> tag? Commented May 30, 2013 at 18:05
  • 1
    Please show the full HTML so we can see the structure of the form. Commented May 30, 2013 at 18:05
  • also script not in a script tag... I think we're missing something Commented May 30, 2013 at 18:05
  • 1
    Actually I did not mention the HTML because HTML and Scripts everything was okay. Anyway I find the solution although I don't know why the above jquery function was not working. But following is working.... $("form").submit(function(e) { if(!$('input[type=checkbox]:checked').length) { e.preventDefault(); alert("No entry was selected!"); } return true; }); Commented May 30, 2013 at 18:09
  • Not sure if there is any problem in your html structure, but name="..." in the input tags matters when submitting through a form. You are using the same name for all of them, and it would probably cause an error Commented May 30, 2013 at 18:10

2 Answers 2

1

Give this a whirl:

$("form").submit(function(e) {
  if(!$('input[type=checkbox]:checked').length) {
    e.preventDefault();

   alert("No entry was selected!"); }
return true; });
Sign up to request clarification or add additional context in comments.

Comments

0

You are using the return true into the each loop. Using return in the each loop just mean if the loop continue (on true) or if it stop (on false).

That beign said, you script will alway roll hover a return false, preventing the form from submiting.

What you could do

If you want to keep the loop, save if it's true or false in a var!

var isChecked = false;
$('.chk_delete').each(function(){
    isChecked = $(this).is(':checked')    
    return !isChecked;
});
return isChecked;

OR

Use your working script, I don't see what's wrong with it

$("form").submit(function(e) { 
    if(!$('input[type=checkbox]:checked').length) { 
        e.preventDefault(); 
        alert("No entry was selected!");
    } 
    return true;
});

$('input[type=checkbox]:checked') select every checked box. So $('input[type=checkbox]:checked').length take the length. Having ! is the same thing as !=.

Finally, !$('input[type=checkbox]:checked').length is a shotcut for $('input[type=checkbox]:checked') != 0. If true, it .preventDefault() wich prevent the form of submiting.

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.