문제

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