Вопрос

<script>
function no_email_confirm() { 
  if (document.getElementsByName("no_email")[0].checked == false) {
    return true;
  } else {
   var box= confirm("Sure?");
    if (box==true)
        return true;
    else
       document.getElementsByName("no_email")[0].checked == false;
  }
}

</script>

And here is my HTML for the checkbox:

 <input type="checkbox" id="no_email"  name="no_email" onchange="no_email_confirm()"></input>

For some reason, this gives me the confirm pop up the first time I check the box, but not for any click after that. Also, even if I click "Cancel" it still checks the check box. I've searched on here and for some reason, no matter what I try, I can't get it to work properly.

It should confirm if they really want to check the box, if they select "Yes" then it checks it, if not, then it doesn't check it. I can get it to work without the name no_email, but I can't change that..

Anyone have any ideas?

Это было полезно?

Решение

Looks like you've got several errors in there, most notably using == when you probably meant =. Instead, add an event listener and make sure the assignment works:

var box = document.querySelector('#no_email');

box.addEventListener('change', function no_email_confirm() { 
  if (this.checked == false) {
    return true;
  } else {
   var confirmation= confirm("This means that the VENDOR will NOT RECEIVE ANY communication!!!!");
    if (confirmation)
        return true;
    else
       box.checked = false;
  }
});

http://jsfiddle.net/A3VGg/1/

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top