简体   繁体   English

无法读取带有value,innerHTML或checked复选框的值

[英]Can't read value of checkbox with value, innerHTML or checked

I'm doing a basic (becuse I'm not skilled and just like to play around bulidning small things to learn) database project, setting things by checking checkboxes. 我正在做一个基础的数据库项目(因为我不熟练,只是想学习一些有趣的小知识),通过选中复选框来设置内容。 However, when trying to check a box and read if it is checked or not checked it always reads "null". 但是,当尝试选中一个框并读取它是否被选中时,它始终显示为“ null”。 I've tried .value , .innerHTML and .checked . 我已经尝试过.value.innerHTML.checked Nothing gets me there. 没有什么让我在那里。 All I'm trying to do is to pick up the input (ie checked or not checked) with getElementById and store it in a var, and later on compare if the boolean is true och false to set the value in the databas accordingly. 我要做的就是用getElementById提取输入(即选中或未选中)并将其存储在var中,然后比较boolean是否为true或false以相应地在数据库中设置值。

function createSheet() {

var _container = document.getElementById("container");

checkbox = document.createElement("input");
    checkbox.type = "checkbox";
    var valueOfId = document.getElementById("idString").checked;

Error messege: Cannot read property 'checked' of null. 错误信息:无法读取null的“已检查”属性。

EDIT: Thanks for the answers! 编辑:感谢您的答案! I didn't show these lines since I didn't think they mattered that much, but I was wrong so here they are: 我没有显示这些行,因为我认为它们没那么重要,但是我错了,所以这里是:

function createSheet() {
var _container = document.getElementById("container");
for (var i = 0; i < 4; i++) {
    p = document.createElement("p");
    spanBird = document.createElement("span");
    checkbox = document.createElement("input");
    checkbox.type = "checkbox";
    checkbox.id = "myCheck" + i;
    checkbox.value = 0;
    checkbox.checked = birds.birdList[i].seen;
    spanBird.innerHTML = birds.birdList[i].name;
    p.appendChild(spanBird);
    p.appendChild(checkbox);
    container.appendChild(p);
    console.log(document.getElementById("myCheck" + i));
    document.getElementById("myCheck" + i).addEventListener("click", readBox);
    }
}

function readBox(){
getId();
theId = theId.substring(7);
console.log("idString: " + idString);
var valueOfId = document.getElementById("idString").checked;
}

and the HTML reads: HTML读取:

<section id="container">

    </section>

/EDIT /编辑

Any suggestions? 有什么建议么?

Couple things: 几件事情:

  • You don't need document.getElementById , since you have checkbox already. 您不需要document.getElementById ,因为您已经有checkbox
  • As stated by others, to use document.getElementById, you need to set a id to the checkbox Element. 如其他人所述,要使用document.getElementById,您需要为复选框Element设置一个ID。 And you also need to add the element to the document. 并且您还需要将元素添加到文档中。

 checkbox = document.createElement("input"); checkbox.type = "checkbox"; checkbox.id = "idString" console.log(checkbox.checked) document.body.appendChild(checkbox) console.log(document.getElementById("idString").checked) 

Actually, it's not that the property checked is null, your parent object is null. 实际上,不是检查的属性为null,而是父对象为null。

I mean, this part: 我的意思是,这部分:

document.getElementById("idString")

is returning null, so it can't access the checked property. 返回null,因此它无法访问checked属性。

You need to first set the element's id with this 您需要首先使用此设置元素的ID

checkbox = document.createElement("input");
checkbox.type = "checkbox";
checkbox.setAttribute("id", "idString");
document.body.appendChild(checkbox)
var valueOfId = document.getElementById("idString").checked;

Then you should be able to access the value 然后,您应该可以访问该值

In order to work you id first need create the attributes for your element like this. 为了正常工作,您首先需要为元素创建属性,如下所示。

You cant call by id if it isnt instantiate. 如果没有实例化,则无法通过id调用。 Try out. 试用。

HTML 的HTML

<div id="content"></div>    

JAVASCRIPT JAVASCRIPT

  checkbox = document.createElement("input");
  checkbox.setAttribute("id", "idString");
  checkbox.setAttribute("type", "checkbox");
  checkbox.setAttribute("value", "0");

   var element = document.getElementById("content");
   element.appendChild(checkbox);
   var valueOfId = document.getElementById("idString").value;
   console.log(valueOfId);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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