简体   繁体   English

不使用onclick和JavaScript的按钮

[英]Buttons not working with onclick and JavaScript

I am trying to make buttons that adds 10, 100, and 1000 to an input and that can clear the input field, but i can't get it to work. 我正在尝试制作一个按钮,为输入添加10,100和1000,并且可以清除输入字段,但我无法使其工作。 I have tried alle the different ways i know to add the script to my document(inline and external) but i can't seem to crack the code. 我已经尝试了不同的方法,我知道将脚本添加到我的文档(内联和外部),但我似乎无法破解代码。 The debug console tells me that The functions is not defined 调试控制台告诉我没有定义函数

EDIT: 编辑:

The problem seems to be that when clicking the buttons the site redirects to the index file 问题似乎是当点击按钮时,网站会重定向到索引文件

 var bet = document.getElementById('coincredits'); var cur = document.getElementById('coincredits').value; var coins = document.getElementById('coins').value; function clear() { bet.value = ""; } function ten() { bet.value = cur + 10; } function hundred() { bet.value = cur + 100; } function thousand() { bet.value = cur + 1000; } function xtwo() { bet.value = cur * 2; } function max() { bet.value = coins; } 
 <h4>Coins:</h4><p id="coins">123456789</p> <input type="number" name="coincredits" id="coincredits" required="" parsley-type="text" data-parsley-id="40"> <div> <button onclick="clear()">Clear</button> <button onclick="ten()">+10</button> <button onclick="hundred()">+100</button> <button onclick="thousand()">+1000</button> <button onclick="xtwo()">x2</button> <button onclick="max()">Max</button> </div> 

You need to get the current value every time one of your functions is called. 每次调用一个函数时,都需要获取当前值。 Right now you are getting the current value of your input just once (when the script is evaluated). 现在,您只需获取输入的当前值(在评估脚本时)。

ie add the line below to each of your functions: ten, hundred, thousand and xtwo 即将以下行添加到您的每个功能:十,百,千和xtwo

var cur = document.getElementById('coincredits').value;

The issue here is you only set the value of cur when the document loads. 这里的问题是您只在文档加载时设置cur的值。

You need to set cur to the new value before running each function. 在运行每个函数之前,需要将cur设置为新值。

You also need to parse your value to a number, here its an int 您还需要将值解析为数字,此处为int

 var bet = document.getElementById('coincredits'); var cur = document.getElementById('coincredits').value; var coins = document.getElementById('coins').value; function updateCur() { var value = parseInt(document.getElementById('coincredits').value) if (!value) value = 0; cur = value; } function clear() { bet.value = ""; updateCur(); } function ten() { updateCur(); bet.value = cur + 10; } function hundred() { updateCur(); bet.value = cur + 100; } function thousand() { updateCur(); bet.value = cur + 1000; } function xtwo() { updateCur(); bet.value = cur * 2; } function max() { updateCur(); bet.value = coins; } 
 <h4>Coins:</h4><p id="coins">123456789</p> <input type="number" name="coincredits" id="coincredits" required="" parsley-type="text" data-parsley-id="40"> <div> <button onclick="clear()">Clear</button> <button onclick="ten()">+10</button> <button onclick="hundred()">+100</button> <button onclick="thousand()">+1000</button> <button onclick="xtwo()">x2</button> <button onclick="max()">Max</button> </div> 

1) put console.log('test') in the 1-st line of your javascript code, maybe it doesn't loads 1)将console.log('test')放在你的javascript代码的第一行,也许它不会加载

2) when browser renders and binds your html code maybe he doesn't know anything about this functions 2)当浏览器渲染并绑定你的html代码时,他可能对此功能一无所知

3) your javascript maybe doesn't see any html elements in the document when it implement's in this case you should wrap all your js code into $(function(){ ..... }) - it will start your js code only after DOM will be ready 3)你的javascript可能在它实现时没有看到文档中的任何html元素在这种情况下你应该将你的所有js代码包装成$(function(){.....}) - 它将只启动你的js代码在DOM准备好之后

You have two problems here. 你有两个问题。 One is you are setting the bet and cur variables once on page load and not the latest value. 一个是你在页面加载时设置赌注和cur变量,而不是最新值。 The second is that you need to get the value of coins by doing an innerHTML of the tag. 第二个是你需要通过做一个标签的innerHTML来获得硬币的价值。

I fixed one of your functions, this will help you fix the rest. 我修复了你的一个功能,这将帮助你解决剩下的问题。

function max() {
    var bet = document.getElementById('coincredits');
    var cur = document.getElementById('coincredits').value;
    var coinsTag = document.getElementById('coins');
    var coins= coinsTag.innerHTML;

    bet.value = coins;
}

you have an problem on 你有问题

var coins = document.getElementById('coins').value;

its have to be .innerHTML. 它必须是.innerHTML。

i try to rewrite your function here the complete. 我试着在这里重写你的功能。

 /*declare a function call hAddCoin with parameter hValue for value and option for option +,x2,clear or max*/ function hAddCoin(hValue, option){ var bet = document.getElementById('coincredits'); /*get the element*/ var coins = document.getElementById('coins').innerHTML;/*get the inner of id coins*/ var cur = parseInt(bet.value); /*get the coincredit and convert to integer*/ var res = 0; /*declare res variable for result*/ /*we need to check bet is empty or not*/ if(typeof bet.value === "undefined" || bet.value ==""){ cur = 0; } /*cek the option, it's will be +, X2 or max and default to 0*/ switch(option){ case 1:{ res = cur + hValue; }break; case 2:{ res = cur * option; }break; case 3:{ res = parseInt(coins); }break; default:{ res = 0; }break; } bet.value = res;/*set value coin creadit to result*/ } 
 <h4>Coins:</h4><p id="coins">123456789</p> <input type="number" name="coincredits" id="coincredits" required="" parsley-type="text" data-parsley-id="40"> <div> <button onclick="hAddCoin(0,0)">Clear</button> <button onclick="hAddCoin(10,1)">+10</button> <button onclick="hAddCoin(100,1)">+100</button> <button onclick="hAddCoin(1000,1)">+1000</button> <button onclick="hAddCoin(0,2)">x2</button> <button onclick="hAddCoin(0,3)">Max</button> </div> 

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

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