简体   繁体   English

jQuery if()问题

[英]jQuery if() issues

Could someone please tell me whats wrong with the following code? 有人可以告诉我以下代码有什么问题吗? Everything looks good. 一切看起来都很好。 What I'm trying to achieve is getting the Side-Panel to widen on click, then shorten on click if the value is "Normal". 我要达到的目的是使侧面板在点击时变宽,如果值是“ Normal”,则在点击时变短。

$(document).ready(function(){
    $("#widen_btn").click(function(){
        $("#side-panel").css("width", "500px");
        $("#widen_btn").val("Normal");
    });

    $("#widen_btn").click(function(){
        var x = $("#widen_btn").val();
        if(x.val() == "Normal"){
            $("#side-panel").css("width", "250px");
        }
    });
});

You can simplify this a bit.. 您可以稍微简化一下。

$(document).ready(function(){
    $("#widen_btn").click(function() {
        if($(this).val() == "Normal") {
            $("#side-panel").css("width", "250px");
            $(this).val("Narrow");
        } else { 
            $("#side-panel").css("width", "500px");
            $(this).val("Normal");
        }
    });
});

That should work. 那应该工作。

It just uses $(this) , and makes sure that you change the value of the button to something other than "Normal" when it's not set to 500px. 它仅使用$(this) ,并确保在未将按钮的值设置为500px时将按钮的值更改为“ Normal”以外的值。

Here's a 'better' example where we cache the $(this) variable, so we're not wasting time recreating the object. 这是一个“更好”的示例,我们在其中缓存$(this)变量,因此我们不会浪费时间重新创建对象。

$(document).ready(function(){
    $("#widen_btn").click(function() {
        var $widen = $(this);
        if( $widen.val() == "Normal") {
            $("#side-panel").css("width", "250px");
            $widen.val("Narrow");
        } else { 
            $("#side-panel").css("width", "500px");
            $widen.val("Normal");
        }
    });
});

.val() returns a string. .val()返回一个字符串。
x.val() doesn't make sense. x.val()没有任何意义。

Also, your first handler always runs even if it's already widened (you need an if there). 同样,即使第一个处理程序已经扩展了,它也始终会运行(如果需要, if需要一个)。
Also, you need to reset the button text. 另外,您需要重置按钮文本。

You mean like this? 你的意思是这样吗?

$(document).ready(function(){
    $("#widen_btn").click(function(){
        var x = $("#widen_btn").val();
        if(x === "Normal"){
            $("#side-panel").css("width", "250px");
        }else{
            $("#side-panel").css("width", "500px");
        }
    });
});

I guess you should change 我想你应该改变

var x = $("#widen_btn").val();

to

var x = $("#widen_btn");

then 然后

x.val()

does make sense 确实有意义

You are redefining event handler, you can do something like this: 您正在重新定义事件处理程序,可以执行以下操作:

$(document).ready(function(){
var state = 'Not Normal';

$("#widen_btn").click(function(){

       var x = $("#widen_btn");
        if(x.val() == "Normal"){
        $("#side-panel").css("width", "250px");
        $("#widen_btn").val("Not Normal");
        }else{
           $("#side-panel").css("width", "500px");
           $("#widen_btn").val("Normal");
        }

    });
});

Firstly you don't need to assign two click handlers to the same field for this. 首先,您不需要为此将两个点击处理程序分配给同一字段。 Both of those will run on every click so the first will set it wide then the second will (once you fix the if condition) set it back to small. 两者都将在每次单击时运行,因此第一个将其设置为较宽,然后第二个(一旦您修复了if条件)将其设置为较小。

Secondly x.val() doesn't make sense when x is already the (string) value so you want to compare x === "Normal" . 其次,当x已经是(string)值时, x.val()没有意义,因此您想比较x === "Normal" Except that given the x variable is only used in that one place you don't really need it at all, you can just compare .val() === "Normal" . 除了给定x变量仅在一个位置上使用之外,您根本不需要它,您可以比较.val() === "Normal"

So try this: 所以试试这个:

$(document).ready(function(){
    $("#widen_btn").click(function(){
        var $this = $(this);
        if ($this.val() === "Normal") {
           $this.val("Narrow");
           $("#side-panel").css("width","250px");
        } else {
           $this.val("Normal");
           $("#side-panel").css("width", "500px");
        );
    });
});

Note that within the click handler this refers to the clicked element, so I've used $(this) rather than $("#widen_btn") and cached a reference to that in a $this variable. 请注意,在单击处理程序中, this是指clicked元素,因此我使用了$(this)而不是$("#widen_btn")并将对该引用的引用缓存在$this变量中。 If the button in question is an <input type="button"> you can just say this.value directly. 如果所讨论的按钮是<input type="button"> ,则可以直接说出this.value

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

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