简体   繁体   English

未定义onclick函数getInc

[英]onclick function getInc not defined

I am creating my first web app and have run in to an issue pretty early on! 我正在创建我的第一个Web应用程序,并且很早就遇到了问题!

I have created a function which extracts the information that the user keys into the HTML input field. 我创建了一个函数,该函数可将用户键入的信息提取到HTML输入字段中。 The way it should work is that when the user enters their income and clicks the submit button, the income is stored in a variable for me to use throughout the rest of my JavaScript code. 它的工作方式是,当用户输入收入并单击“提交”按钮时,收入将存储在变量中,供我在其余JavaScript代码中使用。 Looking at the console log, the function is coming up as 'not defined'. 查看控制台日志,该功能显示为“未定义”。

I appreciate the code is probably not very clean but I just want to get it working as it's my first small project! 我很欣赏代码可能不是很干净,但是我只是想让它正常工作,因为这是我的第一个小项目!

Any ideas where I am going wrong? 有什么想法我要去哪里吗?

Here's the HTML: 这是HTML:

<div class="row input-1">           
    <label for="Gross Annual Salary">£</label>
    <input type="number" name="Gross Annual Salary" id="Ann-Sal" placeholder="Gross Annual Salary" required>
    <input type="button" class="submit-btn" value="Submit" onclick="getInc();">                                              
</div>

Here's the JavaScript: 这是JavaScript:

function getInc() {
    var inc = document.getElementById("Ann-Sal");
}

I remove semiclon from onclick end and it works 我从onclick端删除分号,并且可以正常工作

 <div class="row input-1"> <label for="Gross Annual Salary">£</label> <input type="number" name="Gross Annual Salary" id="Ann-Sal" placeholder="Gross Annual Salary" required> <input type="button" class="submit-btn" value="Submit" onclick="getInc()"> </div> <script> function getInc() { console.log('works'); var inc = document.getElementById("Ann-Sal"); } </script> 

 <script> function getInc() { console.log('works'); var inc = document.getElementById("Ann-Sal"); } </script> <div class="row input-1"> <label for="Gross Annual Salary">£</label> <input type="number" name="Gross Annual Salary" id="Ann-Sal" placeholder="Gross Annual Salary" required> <input type="button" class="submit-btn" value="Submit" onclick="getInc()"> </div> 

First of all make sure you have your <script> after the body (ie: put it after the last closing <div> ), secondly if you want to what the user typed in you should get the elements value. 首先,请确保您在正文后加上<script> (即:将其放在最后一个结束<div> ),其次,如果您希望用户输入的内容,则应获取elements值。 When you set inc equal to document.getElementById("Ann-Sal"); 当您将inc设置为document.getElementById("Ann-Sal"); you are giving it the DOM element instead of the it's value instead you should put: 您正在给它DOM元素而不是它的值,而应该输入:

function getInc() {
    var inc = document.getElementById("Ann-Sal");
}

Then you will get the value the user inputted to the code. 然后,您将获得用户输入到代码中的值。

Also, a tip use addEventListener() instead of the onclick attribute, it's better practice. 另外,技巧是使用addEventListener()代替onclick属性,这是更好的做法。

Ok, so I tried to fix it myself using some of Tom O's code above here: 好的,所以我尝试使用上面的Tom O的一些代码自己修复它:

var buttonEl = document.querySelector('input[type="button"]');

var inputEl = document.getElementById("Ann-Sal");

var annInc;

function clickHandler() {

    annInc = parseFloat(inputEl.value);
    console.log(annInc);
}

buttonEl.addEventListener('click', clickHandler);

HTML: HTML:

<div class="row input-1">

                        <label for="Gross Annual Salary">£</label>
                        <input type="number" name="Gross Annual Salary" id="Ann-Sal" placeholder="Gross Annual Salary" required>
                        <input type="button" class="submit-btn" value="Submit">


                    </div>

This didn't work. 这没用。 There was no error message but nothing was logged to the console when submitting an income into the input field. 没有错误消息,但是将收入提交到输入字段时,没有任何内容记录到控制台。

I then reconfigured the Javascript code to this: 然后,我将Javascript代码重新配置为:

var buttonEl = document.querySelector('input[type="button"]');

var inputEl = document.getElementById("Ann-Sal");

var annInc;

function clickHandler() {
    if (!inputEl.value.length) {
        console.error('A salary is required!');
        return;
    }

    annInc = parseFloat(inputEl.value);

    console.log(annInc);
}

buttonEl.addEventListener('click', clickHandler);

This then worked. 然后工作了。 I didn't use the if statement to validate the users input previously because I used 'required' in the input element within my HTML code which basically does the same thing right? 我之前没有使用过if语句来验证用户输入,因为我在HTML代码中的input元素中使用了“ required”,这基本上是对的吗? So i'm guessing the reason why the code wasn't working because the 'return' statement wasn't used? 所以我正在猜测为什么由于未使用'return'语句而导致代码无法正常工作的原因?

Would someone be kind enough to explain to me why the return statement enabled this to work? 有人会友好地向我解释为什么return语句使此方法有效吗?

I'm sure for some of you experienced guys, my lack of understanding must be painful for you! 我敢肯定,对于你们中一些经验丰富的人,我缺乏理解一定会让您痛苦不已! I am super determined to get my head around this though. 我非常决心要解决这个问题。

Many thanks 非常感谢

Besides the other answers, I just wanna point it out that is missing the .value in your function, otherwise, you won't get the value. 除了其他答案,我只想指出函数中缺少.value情况,否则您将无法获得该值。

function getInc() {
    var inc = document.getElementById("Ann-Sal").value
}

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

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