Simple JavaScript Checkbox Validation

前端 未结 11 1994
长情又很酷
长情又很酷 2020-12-10 03:56

I usually work with PHP so sadly don\'t have some basic JS principles down. This is all I want to accomplish--I\'ve seen many posts on this topic but they are usually beyon

11条回答
  •  既然无缘
    2020-12-10 04:17

    You could use:

     if(!this.form.checkbox.checked)
    {
        alert('You must agree to the terms first.');
        return false;
    }
    

    (demo page).

    
    
    
    • Returning false from an inline event handler will prevent the default action from taking place (in this case, submitting the form).
    • ! is the Boolean NOT operator.
    • this is the submit button because it is the element the event handler is attached to.
    • .form is the form the submit button is in.
    • .checkbox is the control named "checkbox" in that form.
    • .checked is true if the checkbox is checked and false if the checkbox is unchecked.

提交回复
热议问题