简体   繁体   English

如何在javascript中将输入值设置为对象属性的值

[英]How to set the input value to be a object's property's value in javascript

I want to set the input value to be a object's property's value, here is the code in jsfiddle , 我想将输入值设置为对象属性的值,这是jsfiddle中的代码,
I also put the code here: 我也将代码放在这里:
html html

<div id="contenu"></div>  

js js

// create the button
var contenu = document.getElementById("contenu");
var btn = document.createElement("button");
btn.textContent = "add";
contenu.appendChild(btn);

// click the button to create 2 inputs and submit
btn.addEventListener("click", function(){
    // create 2 inputs and submit
    var name = document.createElement("input");
    name.placeholder = "enter a name";
    var age = document.createElement("input");
    age.placeholder = "enter the age";
    var submit = document.createElement("input");
    submit.type = "submit";
    submit.value = "submit";
    var form = document.createElement("form");
    form.method = "post";
    form.appendChild(name);
    form.appendChild(age);
    form.appendChild(submit);
    contenu.appendChild(form);

    // create a new empty table
    var newTable = [];
    var newObject = {};
    newObject.name = name.value;//"Tom" will work
    newObject.age = age.value;//20 will work
    newTable.push(newObject);

    // submit the form to show the input value in html
    form.addEventListener("submit", function(e){
        newTable.forEach(function(table){
            e.preventDefault();
            var pElt = document.createElement("p");
            pElt.innerHTML = table.name + " " + table.age;  
            contenu.appendChild(pElt);
        });   
    });
});

Objective: click the "add" button to show 2 inputs and the "submit" button, enter the name and the age, then click "submit" to show them in the div , I put the input's value as the property's value of the object, newObject.name = name.value; 目标:单击“添加”按钮以显示2个输入,然后单击“提交”按钮,输入名称和年龄,然后单击“提交”以在div显示它们,我将输入的值作为对象的属性值, newObject.name = name.value; and newObject.age = age.value; newObject.age = age.value; but it dosen't work, thanks for your help! 但这没用,谢谢您的帮助!

I fixed your fiddle, you can check it out here: 我已修复您的小提琴,您可以在此处查看:

https://jsfiddle.net/o08mua1y/ https://jsfiddle.net/o08mua1y/

    form.addEventListener("submit", function(e){
        newObject.name = name.value;//"Tom" will work
        newObject.age = age.value;//20 will work
        newTable.forEach(function(table){
            e.preventDefault();
            var pElt = document.createElement("p");
            pElt.innerHTML = table.name + " " + table.age;  
            contenu.appendChild(pElt);
        });   
    });

The issue is that you are trying to set the value of the name, and age field in the listener that creates your input fields, not in the listener that fires on submission of the inputs. 问题是您试图在创建输入字段的侦听器中设置名称和年龄字段的值,而不是在提交输入时触发的侦听器中设置名称和年龄字段的值。 If you set those values inside the submit listener, before the forEach loop the values will be set, and be displayed in your HTML as intended 如果在提交侦听器中设置这些值,则将在forEach循环之前设置这些值,并按预期在HTML中显示

name and age do not have a value at the time that you create the newObject . nameage在创建newObject时没有值。 You need to attach an event listener like keyup or keydown or change to the input field to fire a function to set the values in the object. 您需要附加一个事件侦听器(例如keyupkeydown或者change为输入字段,以激发一个函数来设置对象中的值。

Not sure what you did there but this makes it work. 不知道您在那里做了什么,但这使它起作用。 Take a look. 看一看。

// create de button
var contenu = document.getElementById("contenu");
var btn = document.createElement("button");
btn.textContent = "add";
contenu.appendChild(btn);

// click the button to create 2 inputs and submit
btn.addEventListener("click", function(){
// create 2 inputs and submit
var name = document.createElement("input");
name.placeholder = "enter a name";
var age = document.createElement("input");
age.placeholder = "enter the age";
var submit = document.createElement("input");
submit.type = "submit";
submit.value = "submit";
var form = document.createElement("form");
form.method = "post";
form.appendChild(name);
form.appendChild(age);
form.appendChild(submit);
contenu.appendChild(form);

// create a new empty table
var newTable = [];


// submit the form to show the input value in html
form.addEventListener("submit", function(e){
  var newObject = {};
  newObject.name = name.value;//"Tom" will work
  newObject.age = age.value;//20 will work
  newTable.push(newObject);
  var pElt = document.createElement("p");
  pElt.innerHTML = newObject.name + " " + newObject.age;  
  contenu.appendChild(pElt);   
  e.preventDefault();
});
});

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

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