简体   繁体   English

在 integer 为负数时停止运行 onclick function

[英]Stop a onclick function from running while an integer is negative number

I am building this stats system where you click to assign stats from a pool of available stat points to distribute.我正在构建这个统计系统,您可以在其中单击以从可用统计点池中分配统计信息以进行分发。 I have written down the JS functions using increments and the important part in this whole block is the "Stats Remaining".我已经使用增量写下了 JS 函数,整个块中的重要部分是“Stats Remaining”。 Right now if you run out of stats, it will continue to assign stats and go into negative "Stats Remaining".现在,如果您用完统计数据,它将继续将统计数据和 go 分配为负的“剩余统计数据”。 How do i build the logic of my functions around stopping them at 0. I don't want to permanently stop the function, just don't let to assign anymore while the remaining stats are 0. Can i do it with the current way of doing things or should i re-write the whole logic of the stats distribution.我如何围绕将它们停止在 0 来构建我的函数的逻辑。我不想永久停止 function,只是在剩余统计数据为 0 时不要再分配。我可以用当前的方式来做吗做事还是应该重写统计分布的整个逻辑。 Can I have suggestions please?请问我可以有建议吗?

function addStr() {
  str[0].value++;
  statsRemaining[0].value--;
}
function addDex() {
  ++dex[0].value;
  --statsRemaining[0].value;
}
function addVit() {
  vit[0].value++;
  statsRemaining[0].value--;
}
function addEne() {
  ene[0].value++;
  statsRemaining[0].value--;
}
function resetStats() {
  str[0].value = str[0].defaultValue;
  dex[0].value = dex[0].defaultValue;
  vit[0].value = vit[0].defaultValue;
  ene[0].value = ene[0].defaultValue;
  statsRemaining[0].value = statsRemaining[0].defaultValue;
}

And here is the HTML这是 HTML

<p>Strength</p>
        <input type="number" value="25" name="str" id="str" readonly /><span style="font-size: 26px;cursor:pointer;" onclick="addStr()">+</span>
<p>Dexterity</p>
        <input type="number" value="25" name="dex" id="dex" readonly /><span style="font-size: 26px;cursor:pointer;" onclick="addDex()">+</span>
<p>Vitality</p>
        <input type="number" value="25" name="vit" id="vit" readonly /><span style="font-size: 26px;cursor:pointer;" onclick="addVit()">+</span>
<p>Energy</p>
        <input type="number" value="25" name="ene" id="ene" readonly /><span style="font-size: 26px;cursor:pointer;" onclick="addEne()">+</span>
        <br><br>
<p>Stat points remaining: <input style="width: 42px;" type="number" name="statsRemaining" value="25" readonly /></p><br>
<input type="button" name="reset" value="Reset Stats" onclick="resetStats()" />

Here is a fiddle of it https://jsfiddle.net/Skarsburning/79crgk6b/6/这是它的小提琴https://jsfiddle.net/Skarsburning/79crgk6b/6/

You just need to put a conditional on each of the functions.您只需要对每个函数设置一个条件。

if (statsRemaining > 0) { the increment }; if (statsRemaining > 0) { 增量 };

    // stats
const str = document.getElementsByName('str');
const dex = document.getElementsByName("dex");
const vit = document.getElementsByName("vit");
const ene = document.getElementsByName("ene");
const statsRemaining = document.getElementsByName("statsRemaining");

function addStr() {
    if (statsRemaining[0].value > 0)
  {
    str[0].value++;
    statsRemaining[0].value--;
    }
}
function addDex() {
        if (statsRemaining[0].value > 0)
    {
        ++dex[0].value;
        --statsRemaining[0].value;
        }
}
function addVit() {
    if (statsRemaining[0].value > 0)
  {
    vit[0].value++;
    statsRemaining[0].value--;
    }
}
function addEne() {
    if (statsRemaining[0].value > 0)
  {
    ene[0].value++;
    statsRemaining[0].value--;
  }
}
function resetStats() {
  str[0].value = str[0].defaultValue;
  dex[0].value = dex[0].defaultValue;
  vit[0].value = vit[0].defaultValue;
  ene[0].value = ene[0].defaultValue;
  statsRemaining[0].value = statsRemaining[0].defaultValue;
}

You can also add an 'else' to display a message if you want.如果需要,您还可以添加“else”以显示消息。

The fiddle with just the if statement只有 if 语句的小提琴

https://jsfiddle.net/4Lu5tahg/ https://jsfiddle.net/4Lu5tahg/

Here's the one with a simple alert if there are no points remaining.如果没有剩余积分,这是一个简单的警报。 https://jsfiddle.net/4Lu5tahg/1/ https://jsfiddle.net/4Lu5tahg/1/

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

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