简体   繁体   English

如何使用if语句启用按钮?

[英]How do I make a button enable with an if statement?

<script>
    var tic;
    tic = 0;
</script>

 <script>
    var ongngic;
    ongngic = 1;
</script>

<button onclick="alert(tic=tic+ongngic);">Ice Cream</button>
<button id= "b1" onclick="alert(ongngic=ongngic+1, tic=tic-10);" disabled="disabled">Buy 'Jimmy's Ice Cream Stand' - 10 Ice Cream</button>

<script>
    function myFunction() {
        document.getElementById("b1").enabled = true;
    }
</script>

<script>
    if(var tic > 9){
        myFunction();
    }
</script>

When I click the 'Ice Cream' button 10 times, the id = 1 button should enable but doesn't. 当我单击“冰淇淋”按钮10次时,id = 1按钮应该启用但不能启用。 Can anyone help me? 谁能帮我?

There are a few problems with your code. 您的代码有一些问题。

Firstly remove the var inside the if statment you are just declaring the variable again. 首先删除if语句中的var,您只需再次声明该变量即可。

The code in your script tag is running when the page is loading and the if statment is therfore only checked once. 加载页面时,脚本标记中的代码正在运行,因此仅检查一次if语句。 Instead run a function everytime you click the button. 而是每次单击按钮时都运行一个函数。

<button onclick="iceCreamButton();">Ice Cream</button>

function iceCreamButton(){
    alert(tic=tic+ongngic);

    if(tic > 9){
        myFunction();
    }
}

Also the code inside myFunction is not correct change it to this: 另外myFunction内的代码不正确,将其更改为:

document.getElementById("b1").disabled  = false;

Check this code 检查此代码

<script>
    var tic;
    tic = 0;
</script>

 <script>
    var ongngic;
    ongngic = 1;
</script>


<button onclick="clickFun();">Ice Cream</button>
<button id= "b1" onclick="alert(ongngic=ongngic+1, tic=tic-10);" disabled="disabled">Buy 'Jimmy's Ice Cream Stand' - 10 Ice Cream</button>

<script>
    function myFunction() {     
        document.getElementById("b1").disabled = false;
    }
</script>

<script>
 function clickFun(){
        tic+=ongngic;
        alert(tic);
        if(tic > 9){
            myFunction();
        }
    }

  var tic; tic = 0; var ongngic; ongngic = 1; function myFunction() { document.getElementById("b1").disabled = false; } function cal(){ tic=tic+ongngic; alert(tic); if(tic > 9){ myFunction(); } } 
 <button onclick="cal()">Ice Cream</button> <button id= "b1" onclick="alert(ongngic=ongngic+1, tic=tic-10 );" disabled="disabled">Buy 'Jimmy's Ice Cream Stand' - 10 Ice Cream</button> 

There is couple of problems in your code ( some was mentioned by other guys ), I prefer to use jQuery for dealing with DOM , but as you wrote your code in pure JavaScript , I didn't use jQuery in my code. 您的代码中存在几个问题( 有些人提到过其他问题 ),我更喜欢使用jQuery处理DOM ,但是当您使用pure JavaScript编写代码时,我没有在代码中使用jQuery for many reason you should avoid inline JS , basically don't mix HTML with JS as much as possible. 由于许多原因,您应该避免使用inline JS ,基本上不要将HTMLJS尽可能地混合使用。 you can find useful info about that here on SO: onclick=“” vs event handler 您可以在SO上找到有关此信息的有用信息: onclick =“” vs事件处理程序

If you don't familiar with addEventListener , here is good start to learn about that: EventTarget.addEventListener() 如果您不熟悉addEventListener ,那么这里是开始学习的好开始: EventTarget.addEventListener()

HTML 的HTML

<button id="toggle">Ice Cream</button>
<button id="target" disabled="disabled">Buy 'Jimmy's Ice Cream Stand' - 10 Ice Cream</button>

JS JS

var toggle = document.getElementById('toggle'),
    target = document.getElementById('target'),
    tic = 0,
    ongngic = 1,
    myFunction = function() {
        target.removeAttribute('disabled');
    };

toggle.addEventListener('click', function() {
    tic = tic + ongngic;
    console.log(tic);
    // alert(tic);

    if (tic > 9) myFunction();
}, false);

target.addEventListener('click', function() {
    ongngic = ongngic + 1;
    tic = tic - 10;

    var msg = 'tic:' + tic + ', ongngic: ' + ongngic;
    console.log(msg);
    // alert(msg);
}, false);

jsfiddle jsfiddle

From what I understand you're trying to do, you'll need a click event listener for both buttons that check different things. 据我了解,您正在尝试做的事情,两个按钮都需要单击事件侦听器,以检查不同的事物。

The "ice cream" button when clicked will increment it's own counter and enable the "ice cream stand" button when it hits 10. The "ice cream stand" button when clicked decreases the "ice cream" counter by 10 and increases it's own counter by 1. It also disables itself if the "ice cream" counter is lower than 10 in the end. 单击“冰淇淋”按钮将增加其自身的计数器,并在其达到10时启用“冰淇淋架”按钮。单击“冰淇淋架”按钮会将“冰淇淋”计数器减少10并增加其自身的计数器乘以1。如果“冰淇淋”计数器最后低于10,它也会禁用自身。

 var count_ice_cream = 0; var count_ice_cream_stands = 0; $('btn_ice_cream').addEventListener("click", function(e) { count_ice_cream++; if (count_ice_cream > 9) { $('btn_ice_cream_stands').disabled = false; } update_totals(); }); $('btn_ice_cream_stands').addEventListener("click", function(e) { count_ice_cream_stands++; count_ice_cream = (count_ice_cream - 10); $('btn_ice_cream_stands').disabled = !(count_ice_cream >= 10); update_totals(); }); function update_totals() { $('count_ice_cream').innerHTML = count_ice_cream; $('count_ice_cream_stands').innerHTML = count_ice_cream_stands; } // Shh, don't use this in production function $(s) { return document.getElementById(s) } 
 body { font: 400 1em/1.4 sans-serif; } 
 <button id="btn_ice_cream"> Ice Cream: <b id="count_ice_cream">0</b> </button> <br> <button id="btn_ice_cream_stands" disabled> Ice Cream Stands: <b id="count_ice_cream_stands">0</b> </button> 

I hope this helps - place myFunction() in - 希望对您myFunction()帮助-将myFunction()放在-

onclick="alert(tic=tic+ongngic); myFunction()">Ice Cream</button>

and add this below line in myFunction() - 并将其添加到myFunction()下面的行中-

document.getElementById("b1").disabled = false;

So If you want to enable the button you should set .disabled property as false . 因此,如果要启用按钮,则应将.disabled属性设置为false

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

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