Javascript函数在两个特定值之间反转

时间:2014-05-21 11:32:08

标签: javascript html html5

当我更改数字输入框上的值时,取决于值是增加还是减少它应该更改"总数"根据它是增加还是减少(它花费总分数)

然而,当我在" 9"之间切换时和" 10"它反转(如果我降低数字也会降低总数,反之亦然,当它应该做相反的事情时)

测试网站:http://alboneon.pentacore.se/您必须登录以便用户名:stackoverflow和密码:1234然后按创建字符

来访问它

输入框:

<div style="width: 45%;right: 0; float: right; padding-right: 10px; text-align: right;">
    <!--<button onchange="calcstat()">Randomize</button>-->Stat <br/>
    <input  id="strcrt" type="number" max="15" min="8" value="8" onchange="calcstat(this.id, this.value)"><br/>
    <input  id="dexcrt" type="number" max="15" min="8" value="8" onchange="calcstat(this.id, this.value)"><br/>
    <input  id="concrt" type="number" max="15" min="8" value="8" onchange="calcstat(this.id, this.value)"><br/>
    <input  id="intcrt" type="number" max="15" min="8" value="8" onchange="calcstat(this.id, this.value)"><br/>
    <input  id="wiscrt" type="number" max="15" min="8" value="8" onchange="calcstat(this.id, this.value)"><br/>
    <input  id="chacrt" type="number" max="15" min="8" value="8" onchange="calcstat(this.id, this.value)"><br/>
    <div id="total">27</div>
</div>

使用Javascript:

function calcstat(id, values) {
    var change = 0 * 1;
    if (oldscores[id] < values) {
        //change = values - 8;
        change = 1 * 1;
    } else if (oldscores[id] > values) {
        //change = -Math.abs(oldscores[id] - 8);
        change = change-1;
    }
    atributescore = atributescore - change;
    oldscores["strcrt"] = document.getElementById("strcrt").value;
    oldscores["dexcrt"] = document.getElementById("dexcrt").value;
    oldscores["concrt"] = document.getElementById("concrt").value;
    oldscores["intcrt"] = document.getElementById("intcrt").value;
    oldscores["wiscrt"] = document.getElementById("wiscrt").value;
    oldscores["chacrt"] = document.getElementById("chacrt").value;
    document.getElementById("total").innerHTML = atributescore;

1 个答案:

答案 0 :(得分:1)

function calcstat(id, values) {
    var change = values - oldscores[id];
    atributescore = atributescore - change;
    oldscores["strcrt"] = document.getElementById("strcrt").value;
    oldscores["dexcrt"] = document.getElementById("dexcrt").value;
    oldscores["concrt"] = document.getElementById("concrt").value;
    oldscores["intcrt"] = document.getElementById("intcrt").value;
    oldscores["wiscrt"] = document.getElementById("wiscrt").value;
    oldscores["chacrt"] = document.getElementById("chacrt").value;
    document.getElementById("total").innerHTML = atributescore;
}