2017-05-05 46 views
0

我有这样的代码:按钮加载索引文件上点击

$(function() { 
 
    /*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*/ 
 
    } 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type="number" name="coincredits" id="coincredits" class="form-control" required="" parsley-type="text" placeholder="Minimum 10 coins" data-parsley-id="40" style="text-align:center; color: ;"> 
 
<div class="content" style="text-align:center;"> 
 
    <button id="clear" class="box-btn" onclick="hAddCoin(0,0)">Clear</button> 
 
    <button id="add10" class="box-btn" onclick="hAddCoin(10,1,)">+10</button> 
 
    <button id="add100" class="box-btn" onclick="hAddCoin(100,1)">+100</button> 
 
    <button id="add1000" class="box-btn" onclick="hAddCoin(1000,1)">+1000</button> 
 
    <button id="double" class="box-btn" onclick="hAddCoin(0,2)">x2</button> 
 
    <button id="max" class="box-btn" onclick="hAddCoin(0,3)">Max</button> 
 
</div>

它增加了(或者应该增加)值输入栏,但它只是把我送到mywebsite.com/ index.php

我试过了脚本,但每次都发生这种情况。它不会给我任何错误,并在控制台中记录任何内容。

我知道这可能是一块蛋糕,但我无法弄清楚。

+0

尝试删除\t $(函数()在窗体上添加一个onsubmit事件处理程序{....从脚本 – tech2017

回答

1

<button>元素是隐含type="submit"这意味着他们提交他们居住在形式。如果你的<form>没有一个action属性将使用当前页的目标URL,从而重新加载页面。

您需要明确地设定type="button"上的每个按钮或调用event.preventDefault()

+0

比方说我有两页,index.php和page.php这些按钮在page.php上,但它会到index.php。它仍然是相同的情况吗? – McMuffin

+0

如果它加载了一个不同的页面,意味着'action'被设置为某个东西,如果它指向你的index.php,我的猜测是它是'action =“/”'。 –