简体   繁体   English

我在使用 HTML 和 JS 提取用户输入并将其写入页面时遇到问题。 你能指出我在这里做错了什么吗?

[英]I am having a problem pulling the user's input and writing it into a page with HTML and JS. Are you able to point out what I am doing wrong here?

Good morning!早上好!

I am relatively new to the programming game and have just started on my first JS project.我对编程游戏比较陌生,刚刚开始我的第一个 JS 项目。 I have decided to do a pretty basic budget app.我决定做一个非常基本的预算应用程序。 I am having a hard time writing the user input to the page.我很难将用户输入写入页面。 I assume it has something to do with the input itself or how I call the incomingInput var, because I am unable to log it to the console when I switch the Event Listener around to do that.我认为它与输入本身或我如何调用传入输入变量有关,因为当我切换事件侦听器来执行此操作时,我无法将其记录到控制台。 I will post the portion of the code that is relevant to this question below.我将在下面发布与此问题相关的代码部分。 I would appreciate any help you can give.我将不胜感激您能提供的任何帮助。 Thanks!谢谢!

 let incomingInput = document.querySelector("incoming_Cash_Input"); let expenseInputName = document.querySelector("expense_Name"); let expenseInputAmount = document.querySelector("expense_Value"); function incomingCash() { document.getElementById("budgetIncoming").innerHTML = incomingInput; console.log("hello"); } incomingCalcButton.addEventListener("click", incomingCash())
 <h1>Budget Application</h1> <div id="inputSections" class="inputSections"> <section id="incomingMoney"> <h3>Please Enter Your Budget</h3> <input type="number" id="incomingCashInput" class="inputs" name="incoming_Cash_Input"><br> <button id="incomingCalcButton">Calculate</button> </section> <section id="enterExpenses"> <h3>Please Enter Your Expense</h3> <input type="number" id="expenseName" class="inputs" name="expense_Name"> <h3>Please Enter Expense Amount</h3> <input type="number" id="expenseAmount" class="inputs" name="expense_Value"><br> <button id="expenseButton">Add Expense</button> </section> </div> <section id="calculations" class="calcs"> <div class="budget calcs"> <h3>Budget</h3><br> <img src="money_icon.png" class="moneyIcon calcIcon"><br> <section id="budgetIncoming"> </section> </div> <div class="expenses calcs"> <h3>Expenses</h3><br> <img src="expense_icon.png" class="expenseIcon calcIcon"><br> <section class="expenseIncoming"> </section> </div> <div class="balance calcs"> <h3>Balance</h3><br><br> <img src="budget_icon.png" class="budgetIcon calcIcon"><br> <section class="balanceIncoming"> </section> </div> </section>

I've tested your code and made a few adjustments:我已经测试了您的代码并做了一些调整:

let incomingInput = document.querySelector("#incomingCashInput");
let expenseInputName = document.querySelector("#expenseName");
let expenseInputAmount = document.querySelector("#expenseAmount");

let incomingCalcButton = document.querySelector("#incomingCalcButton");

function incomingCash(input) {
    document.getElementById("budgetIncoming").innerHTML = input.value;
}

incomingCalcButton.addEventListener("click", function(){
    incomingCash(incomingInput);
});

So for the querySelector I've added the ID's to select the elements.所以对于 querySelector,我添加了 ID 来选择元素。 I've also added the incomingCalcButton for the eventlistener and added the value of the elements to the innerHTML instead of the entire elements.我还为事件侦听器添加了incomingCalcButton 并将元素的值添加到innerHTML 而不是整个元素。

That should work :)那应该工作:)

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

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