【问题标题】:Multiple buttons for same function with if statement具有 if 语句的相同功能的多个按钮
【发布时间】:2015-09-15 09:23:21
【问题描述】:

我尝试制作自己的案例功能,但一旦我在第一次点击时隐藏它们,我就无法让它“显示东西”。所以我的问题是我做错了什么,我该如何解决,我应该怎么做呢?

我唯一的要求是多个按钮可以使用相同的代码来显示/隐藏相同的对象——因此像oddClick 这样的东西将不起作用,因为这需要不必要的点击才能再次获得偶数/奇数(我认为? )。

        $('.boxbtn').on('click', function () {
        var boxwidth = $('.box').width();
        console.log(boxwidth);
        console.log('-'+boxwidth+'px');
        var state = 1;

        if(state == 0) {  // I think we are trying to compare value here.
/*              alert("foo"); */
            var state = 1;
            console.log(state);
            /*show stuff here*/
        }
        else {
/*              alert("bar"); */
            var state = 0;
            console.log(state);
            /*hide stuff here*/
        }

    });

【问题讨论】:

  • 在一个按钮上使用切换或切换类不需要多个按钮
  • 谷歌搜索您问题的关键字可能会有所帮助。很多答案。
  • 第 5 行:var state = 1; 使其始终进入“else”
  • @Pierre 在这种特定情况下,我确实希望多个按钮做同样的事情,这就是整个想法——无论使用顺序如何,两个按钮都必须执行相同的功能。
  • @Lenny,现在试试我的答案。

标签: javascript jquery button switch-statement case


【解决方案1】:

这行 5: var state = 1;导致它总是进入“else

var state = 1; //placed on global scope
 $('.boxbtn').on('click', function () {
        var boxwidth = $('.box').width();
        console.log(boxwidth);
        console.log('-'+boxwidth+'px');
        //var state = 1; removed from function scope

        if(state == 0) {
/*              alert("foo"); */
            state = 1;
            console.log(state);
            /*show stuff here*/
        }
        else {
/*              alert("bar"); */
            state = 0;
            console.log(state);
            /*hide stuff here*/
        }

    });

小提琴:http://jsfiddle.net/8ubk5c0f/

【讨论】:

  • 感谢您的回答,但这也不起作用?
  • 再试一次,也有 Oliver 发现的小问题
  • 仍然只为我运行 else
  • 确实,您更新后的解决方案完美无缺。干杯伙伴,你让我省了很多头痛!
【解决方案2】:

为了检测对象的状态,您应该使用对象属性(而不是并行的state 变量)。

例如使用boxwidth 的值。

【讨论】:

    【解决方案3】:

    以更简单的方式:

    $('.boxbtn').on('click', function () {
        var boxwidth = $('.box').width(); //Not sure why you want this
        console.log(boxwidth);
        console.log('-'+boxwidth+'px');
        var btnVal=$(this).text(); //get the button text
        if(btnVal=="Show Stuff") //if it is show stuff
        {
            $(this).text('Hide Stuff'); //change its text
            alert('stuff shown');
            /*show stuff here*/ //do the functionality
        }
        else {
            $(this).text('Show Stuff'); //change back to normal
            alert('stuff hidden');
            /*hide stuff here*/ //hide functionality
        }
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <button type="button" class="boxbtn">Show Stuff</button>

    【讨论】:

      【解决方案4】:

      在 click 函数之外声明 state 变量。

      var state = 1; // Do not write inside click function scope
      

      【讨论】:

        【解决方案5】:
        var state = 0; //placed on global scope
        
        $('.boxbtn').on('click', function () {
            var boxwidth = $('.box').width();
            console.log(boxwidth);
            console.log('-'+boxwidth+'px');
        
            if(state == 0) {
                state=1;
                console.log(state);
            }
            else {
                state = 0;
                console.log(state);
            }
        
        });
        

        【讨论】:

        • 更改了这一行 if(state == 0)
        • @saar 请在发帖前检查您的答案。正如我注意到的那样,Guruprasad Rao 的答案是最合适的答案。不推荐使用全局变量。但作为一个快速修复,这没关系。
        • 这只是时间问题。你有 15 分钟的时间来改变你的答案。最后,这不是关于获得积分的事情。这是解决一些人头痛的问题。
        猜你喜欢
        • 1970-01-01
        • 2016-05-11
        • 1970-01-01
        • 2018-03-03
        • 1970-01-01
        • 1970-01-01
        • 2023-03-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多