简体   繁体   English

document.getElementById()始终返回null

[英]document.getElementById() is always returning null

I'm trying to get the value of a hidden input from a form in my HTML, using javascript. 我正在尝试使用javascript从HTML表单中获取隐藏输入的值。 But after trying several different ways to get the value, it always says in console(firebug) that the variable is null.. heres the javascript: 但是在尝试了几种不同的方法来获取值之后,它总是在console(firebug)中说该变量为null。

var mcq_test = form.mcq_test.value;
console.log("mcqid=" + mcq_test)
var mcq_num_questions = form.mcq_num_questions.value;
console.log("totalquestions=" + mcq_num_questions)
var x = 1;
var send = [];
send.push("mcq_test=");
send.push(mcq_test);

console.log("Send: " + send);

// have commented out the bits below...just go through them carefully looking at the string functions, put them in the send += (SRING 1 + STRING 2 + ...) format

for (var x = 1; x <= mcq_num_questions; x++) {
var questionidd = "mcq_question_id";
console.log("1 = " + questionidd);
var questionid = questionidd.concat(x); // mcq_question_id$ctr the question numer
console.log("2 = " + questionid);
var mcqid = form.questionid.value; // the questions id on db
console.log("3 = " + mcqid);
var answerr = "mcq_question";
var answer = answerr.concat(x); // mcq_question$ctr the questions chosen answer
var chosenanswer = form.answer.value; // the answers value
console.log("4 = " + chosenanswer);
var amp = "&";
var equal = "=";
var questionide = questionid.concat(equal); // "mcq_question_id$ctr="
var questionida = amp.concat(questionide); // "&mcq_question_id$ctr="
var answere = amp.concat(answer,equal); // "&mcq_question$ctr="
if (x = 1) { send.push(questionide, mcqid, answere, chosenanswer); }
else {
send.push(questionida, mcqid, answere, chosenanswer);
}
} 

Console: 安慰:

[04:08:00.328] TypeError: questionID is null 
[04:08:00.327] My new function
[04:08:00.327] mcqid=566
[04:08:00.327] totalquestions=3
[04:08:00.327] Send: mcq_test=,566
[04:08:00.328] 1 = mcq_question_id
[04:08:00.328] 2 = mcq_question_id1

Form: 形成:

echo"<input type=\"hidden\" name=\"mcq_question_id$ctr\" id=\"mcq_question_id$ctr\" value=\"$questionID\" form=\"SubmitTest\" />";

-- Update -更新

Answer found! 找到答案! Turns out, you can't use the syntax document.getElementByID() or form.variable.value; 事实证明,您不能使用语法document.getElementByID()或form.variable.value;。 and put a variable in there, it has to be a string or written like this: 并在其中放置一个变量,它必须是字符串或这样写:

form[variable].value;

I didn't find an equivalent for getElementById but the form method works for me. 我没有找到等效的getElementById,但是form方法对我有用。

You need to make sure that the DOM has loaded before you try accessing it. 您需要先确保已加载DOM,然后再尝试访问它。 Put the Javascript after the HTML or setup an event to trigger the variable definition. 将Javascript放在HTML后面,或设置一个事件以触发变量定义。

when using this 当使用这个

echo"<input type=\"hidden\" name=\"mcq_question_id$ctr\" id=\"mcq_question_id$ctr\" value=\"$questionID\" form=\"SubmitTest\" />";

here before going to create input type make sure your '$questionID' is not null 在此处创建输入类型之前,请确保您的'$ questionID'不为null

use this then test 用这个然后测试

echo"<input type=\"hidden\" name=\"mcq_question_id$ctr\" id=\"mcq_question_id$ctr\" value=\"questions\" form=\"SubmitTest\" />";

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

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