Вопрос

I am trying to check boxes in JS. I wan't to only check when the value is equal to 0, if not don't check it. my code :

document.onreadystatechange = function check(e)
{
    if (document.readyState === 'complete')
    {           
        var inputElements = document.getElementsByTagName("input");
        for(var i=0; i < inputElements.length; ++i)
        {               
            if(document.getElementsByTagName("input")[i].value == "1")
            {
              document.getElementById("date").checked = false;
            }        
        }
    }
}

But it doesn't work. Can you help me?

thanks

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

Решение

What you are doing is, if the checkboxes have value of 1, then you are checking it. Make this change in the code and it should work. Also, you need to check if you are doing this on a checkbox and not on a radio or text:

if(document.getElementsByTagName("input")[i].value == 0 && 
   document.getElementsByTagName("input")[i].getAttribute("type") == "checkbox")
{
  document.getElementById("date").checked = true;
}

Другие советы

var eles = document.querySelectorAll("input[type='checkbox']");
var len = eles.length,
    i = 0,
    flag = true;

for (; i < len; i++) {
    if (eles[i].value === "1") {
        flag = false;
        return;
    }
}

if (flag) document.getElementById('date').checked = true;

If the value ( 0 or 1 ) is of an <input type='text'>, then change document.querySelectorAll("input[type='checkbox']"); to document.querySelectorAll("input[type='text']");

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