简体   繁体   English

如何从通过 Javascript function 动态创建的输入字段访问表单输入?

[英]How can I access form input from an input field which was created dynamically via a Javascript function?

I'm new to programming and hope you can help me out with this little number comparison game that I'm trying to build.我是编程新手,希望您能帮助我解决这个我正在尝试构建的小型数字比较游戏。

I have two functions.我有两个功能。 The first function creates the playing field in HTML via a button press.第一个 function 通过按下按钮在 HTML 中创建了运动场。 One of the elements created on the playing field is an input field where the player can enter their guess.在运动场上创建的元素之一是输入字段,玩家可以在其中输入他们的猜测。 In the second function I compare two numbers - the one that was generated randomly and the one that was input by the player.在第二个 function 中,我比较了两个数字——一个是随机生成的,另一个是玩家输入的。 Unfortunately I can't access the number which was entered by the player.不幸的是,我无法访问玩家输入的号码。

Maybe someone has got an idea.也许有人有了主意。 Thank you very much.非常感谢你。

This is what the functions look like:这就是函数的样子:

function startGame(){
(...)
    const inputField = document.createElement("input");
    inputField.setAttribute("type", "number");
    inputField.setAttribute("id", "guess");

    document.getElementById("guess").appendChild(inputField);
}
function compareInput(){
    let inputValue = document.getElementById("guess").value;
(...)
}

I implemented a proof-of-concept, which probably differs from your requirements, but it should help you finding the right path.我实施了一个概念验证,它可能与您的要求不同,但它应该可以帮助您找到正确的路径。 First, let's make sure that the new field has a value.首先,让我们确保新字段具有值。 Next, make sure that we append it to document rather than itself.接下来,确保我们 append 是它来document而不是它本身。

 function startGame(){ const inputField = document.createElement("input"); inputField.setAttribute("type", "number"); inputField.setAttribute("id", "guess"); inputField.value = parseInt(Math.random() * 10) + 1; document.body.appendChild(inputField); } function compareInput(){ let inputValue = document.getElementById("guess").value; console.log(inputValue); } startGame(); compareInput();

  1. You're trying to append "guess" to itself.您正在尝试 append 自己“猜测”。 That doesn't work like that.那不是那样的。 Try to append it to another div or body.尝试将它 append 到另一个 div 或 body。

  2. You have two elements with the same ID.您有两个具有相同 ID 的元素。 The first is the wrapper and the second is the input.第一个是包装器,第二个是输入。 Hence ur function compareInput() is trying to get the value of the wrapper.因此,您的 function compareInput()正在尝试获取包装器的值。

暂无
暂无

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

相关问题 如何动态删除使用JavaScript动态创建的输入字段 - How to dynamically remove input field which is created dynamically using JavaScript 如何使用 vanilla JavaScript 从动态创建的输入字段中获取数据? - How can I get data from dynamically created input field using vanilla JavaScript? 使用jQuery从动态创建的输入字段访问输入文本 - Access input text from dynamically created input field using jquery 如何在JavaScript中为动态创建的输入框分配onkeypress-function? - How can i Assign onkeypress-function to dynamically created input-boxes in javascript? 如何在JavaScript,PHP响应的ajax中动态创建的输入字段中更新特定的输入字段? - How to update a particular input field among dynamically created input fields in JavaScript, ajax from PHP response? 如何从 jQuery/JavaScript 中提取动态创建的输入字段 - How Can I Pull Dynamically Created Input Fields from jQuery/JavaScript 我怎样才能把选择的选项 <select>从动态生成的表单到输入字段 - How can I put the selected option of a <select> from a dynamically generated form into an input field 我有通过javascript动态创建的输入按钮,它应该调用服务器c#函数,但它们不会 - I have input buttons created dynamically by javascript which should call a server c# function but they dont 如何使Google仪表从其上方的函数创建的输入标签读取数据 - How can I make google gauge read data from input tag which was created by a function above it 如何使用使用Javascript动态创建的输入字段? - How to work with input field that is created dynamically using Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM