简体   繁体   English

在onclick操作之后更改Javascript变量值

[英]Changing Javascript variable value after onclick action

Pretty new to this Javascript thing. 这对JavaScript来说还很新。

I want to change a Javascript variable when a user inserts a number into an input field in my HTML document and clicks a button. 当用户在HTML文档的输入字段中插入数字并单击按钮时,我想更改Javascript变量。

I'm assuming you'd use a function, but how do you gather the data and change the variable? 我假设您将使用一个函数,但是如何收集数据并更改变量?

The stuff I tried to make looks a little something like this. 我尝试制作的东西看起来像这样。

HTML 的HTML

<input type="number" id="inputField">
<button onclick="changeTheVariable()" type="button" id="pushMe"></button>

Javascript Java脚本

var a = 0;
function changeTheVariable() {
   a = document.getElementById("inputField").value;
}

but it's not working! 但它不起作用!

Edit 1: 编辑1:

Wow. 哇。 I didn't think I'd get this kind of attention. 我认为我不会受到这种关注。 I also found it a bit strange it didn't work at first. 我还发现它一开始并不起作用,这有点奇怪。

The question I'm asking is partly for a calculator here: https://titomagic.com/debug 我要问的问题部分是在这里的计算器: https : //titomagic.com/debug

It's simple, you type in a number, click the button and it calculates (based on other variables) to a result on the bottom. 很简单,您输入一个数字,单击按钮,它就会(基于其他变量)计算到底部的结果。

Here's a link to the Javascript file, if you wanna have a look: https://titomagic.com/js/bursdagskalkulator.js 如果您想看一下,这是Javascript文件的链接: https : //titomagic.com/js/bursdagskalkulator.js

To those of you asking; 对那些问你的人; yes, I've been testing with a console.log and the variable is not changing. 是的,我一直在使用console.log进行测试,并且变量没有更改。 It's not affecting the other variables (as it should?). 它不会影响其他变量(应该吗?)。

Also I've never heard of JSfiddle. 我也从未听说过JSfiddle。

I discovered few things in the summarizeGjester() function. 我在summarizeGjester()函数中发现了一些东西。 First of all I moved all the Javascript code in the bursdagskalkulator.js file inside the summarizeGjester() function. 首先,我将所有Javascript代码移到了summaryGjester summarizeGjester()函数内的bursdagskalkulator.js文件中。 Also I converted var antallGjester to integer using parseInt() function, because it was treated as string before. 我也使用parseInt()函数将var antallGjester转换为整数,因为以前它被视为字符串。

 var antallGjester = document.getElementById("gjesterAntallInput").value;
 antallGjester = parseInt(antallGjester);  //integer conversion

Also the first Boolean comparison was changed to if ((antallGjester < 10) && (antallGjester > 0)) , so that the second one would work if there's 0 value: else if (antallGjester === 0) . 同样,第一个布尔比较更改为if ((antallGjester < 10) && (antallGjester > 0)) ,以便第二个布尔比较在if ((antallGjester < 10) && (antallGjester > 0))起作用: else if (antallGjester === 0)

  function summarizeGjester() { var antallGjester = document.getElementById("gjesterAntallInput").value; antallGjester = parseInt(antallGjester); var fastPris = 1500; var fastPrisDifferanse = 10; var gjestePris = 120; var gjesteDifferanse = antallGjester - fastPrisDifferanse; var gjesteSum = gjestePris * gjesteDifferanse; var gjesterTotalt = 0; if ((antallGjester < 10) && (antallGjester > 0)) { console.log("antallGjester < 10"); gjesterTotalt = 1500; } else if (antallGjester === 0) { console.log("antallGjester === 0"); gjesterTotalt = 0; } else { console.log("else"); gjesterTotalt = fastPris + gjesteSum; } document.querySelector('#results').innerHTML = gjesterTotalt; } 
 <form> <div class="form-group"> <label for="gjesterAntall">Antall barn:</label> <input type="number" class="form-control" id="gjesterAntallInput"> </div> <button class="btn btn-lg btn-warning" onclick="summarizeGjester()" type="button" id="sumGjester">Legg sammen</button> </form> <h1 class="text-center" style="font-size:80px;"><strong><span id="results">0</span>,-</h1> 

I hope this helps :-) 我希望这有帮助 :-)

HTML 的HTML

<input type="number" id="inputField" ClientIDMode="static">
<button onclick="changeTheVariable()" type="button" id="pushMe"></button>

Javascript Java脚本

var a = 0;
function changeTheVariable() {
   a = document.getElementById('inputField').value;
   alert(a);
}

Use Static ClientIDMode for stable id and access after page rendering PlaceHolders canh change childe's id 在页面呈现PlaceHolders可以更改子代的ID之后,使用静态ClientIDMode获得稳定的ID和访问权限

I suppose this will work for you 我想这对你有用

 var a = 0; function changeTheVariable() { a = document.getElementById("inputField").value || a; document.getElementById("result").innerText = parseFloat(a); } 
 <input type="number" id="inputField"> <button onclick="changeTheVariable()" type="button" id="pushMe">Click me</button> <div>Result: <span id="result"></span></div> 

Edited: 编辑:

The reason behind this code is not running in jsfiddle is here. 此代码背后未在jsfiddle中运行的原因在这里。 在此处输入图片说明

After making the changeTheVariable() global variable this code will work in jsfiddle also. 进行changeTheVariable()全局变量后,此代码也将在jsfiddle中工作。 Here https://jsfiddle.net/1b9cfmje/ 在这里https://jsfiddle.net/1b9cfmje/

Use the following javascript code: 使用以下JavaScript代码:

window.onload = function(){ var a = 0; window.changeTheVariable = function() {  a = document.getElementById("inputField").value || a; document.getElementById("result").innerText = parseFloat(a); }}

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

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