简体   繁体   中英

Validating checkbox is causing troubles

I'm setting up a sign-up form, and user has to accept the conditions before he can submit the form. However, I haven't found a way to succesfully validate the checkbox. It has to be checked, or the user can't continue, and if it's checked --> continue with the script. However, my code isn't working as I want it to, and I'm asking for the correct way to do this.

var Ehdot = $('checkbox[name=ehdot]').is(':checked');
if((Nimi==="")||(ehdot === false)){
if(Ehdot == false){
            alert('Olethan lukenut ehdot ja hyväksyt ne?');
            $('checkbox[name=ehdot]').focus();
            return false;

            }
            return false;
}

A fiddle about this: http://jsfiddle.net/utWYs/1/

If someone dares to say why I'm not using Validate plugin, I kill a kitten. 这个就在这里。

I don't want to use a plugin for this, as it will make the site a bit heavier and I won't learn anything.

A small bug in your code .

var Ehdot = $('checkbox[name=ehdot]').is(':checked');

There is no element named checkbox in your dom . This will returns always false .

you have to change this line to

var Ehdot = $('input[name=ehdot]').is(':checked');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM