变量不适用于appendChild

时间:2017-03-02 21:19:26

标签: javascript

以下是我目前正在使用的代码。我正在创建一个表单并将其值发送到另一台服务器进行处理。当我使用变量作为值时,它不起作用,但是当我对它的值进行硬编码时它起作用。请参阅下文。

此代码包含分配给不起作用的值的变量。

var vfirstname, vlastname;
vfirstname = "Stack";
vlastname = "Overflow";

var mapForm = document.createElement("form");
mapForm.target = "_blank";    
mapForm.method = "POST";
mapForm.action = "https://www.example.com/api/details/";

var firstname = document.createElement("input");
var lastname = document.createElement("input");

firstname.type = "text"; firstname.name = "firstname"; firstname.value = vfirstname;
lastname.type = "text"; lastname.name = "lastname"; lastname.value = vlastname;
mapForm.appendChild( firstname );
mapForm.appendChild( lastname );
document.body.appendChild(mapForm);
mapForm.submit();

但是当我对其工作的值进行硬编码时

firstname.type = "text"; firstname.name = "firstname"; firstname.value = "Stack";
lastname.type = "text"; lastname.name = "lastname"; lastname.value = "Overflow";

0 个答案:

没有答案