Home jquery Checking checkbox for check

Checking checkbox for check

Author

Date

Category

I want to perform validation when submitting a form. But immediately there is a confirmation! Checkbox validation is in progress.

Yes, and one moment, if the checkbox is not set, then an alert is called with a message and a submit occurs, which actually does not need to be done. Here is here scribbled the code.

& lt; form method = "POST" id = "target" & gt;
  & lt; input type = "checkbox" name = "agree" value = "1" id = "agree" checked & gt; & lt; b & gt; & lt; span style = "font-size: 11px;" & gt; I consent to the processing of data
  & lt; input type = "submit" value = "submit" name = "fizsend" id = "subButton" / & gt;
& lt; / form & gt;
& lt; script type = "text / javascript" src = "jquery-1.8.2.js" & gt; & lt; / script & gt;
& lt; script type = "text / javascript" & gt;
$ (document) .ready (function () {
  $ ('# subButton'). on ('click', function () {
    if ($ ("# agree"). attr ("checked") == 'checked') {
      $ ('# target'). submit (function () {});
    } else {
      window.alert ('Give your consent to data processing!');
      $ ("# agree"). css ('border', '1px solid red');
    }
  });
});
& lt; / script & gt;

Answer 1, authority 100%

The onclick handler function must return true or false, depending on the state of the checkbox, for example, like this:

function () {
  if ($ ("# agree"). attr ("checked")! = 'checked') {
    window.alert ('Give your consent to data processing!');
    $ ("# agree"). css ('border', '1px solid red');
    return false;
  }
  return true;
}

EDIT:
Attr (..) method is not supported since jQuery 1.9. Instead, you need to use .prop (…), in your case – with the same parameters.


Answer 2, authority 97%


Answer 3, authority 77%

jQuery validation

$ ("# agree"). is (': checked')

Answer 4, authority 23%

Option without JS (required + CSS)

& lt; style & gt;
 form: invalid input [type = "submit"] {
  opacity: 0.25;
 }
& lt; / style & gt;
& lt; form method = "POST" id = "target" & gt;
  & lt; input type = "checkbox" name = "agree" value = "1" id = "agree" required & gt; & lt; b & gt; & lt; span style = "font-size: 11px;" & gt; I consent to the processing of data
  & lt; input type = "submit" value = "submit" name = "fizsend" id = "subButton" / & gt;
& lt; / form & gt; 

Answer 5, authority 9%

This option will be the most universal, suitable for all versions of Jquery:

if ($ ("# agree"). is (": checked")) {
  $ ('# target'). submit (function () {});
}

Answer 6, authority 5%

here’s another way

& lt; form action = "/" method = "post" onsubmit = "return checkConfirm ()" & gt;
& lt; input type = "checkbox" name = "agree" value = "1" id = "agree" checked & gt; & lt; b & gt; & lt; span style = "font-size: 11px;" & gt; I consent to the processing of data
& lt; input type = "submit" value = "submit" name = "fizsend" id = "subButton" / & gt;
& lt; / form & gt;
& lt; script & gt;
function checkConfirm () {
  if ($ ("# agree"). is (": checked")) {
    return true;
  }
window.alert ('You have not given your consent!');
  return false;
}
& lt; / script & gt;

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions