IF()语句令人费解的行为 [英] Puzzling behavior from IF ( ) statement

查看:98
本文介绍了IF()语句令人费解的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对JavaScript的总数是n00b,并且完全期望这是非常明显的东西,但我不明白为什么if()语句不会产生正确的结果。

无论输入多少个字符,函数都会返回'yes'给控制台,无论'word'是否为<或> 2.如果(哈!)任何人都可以摆脱这一点非常感谢。代码如下:

  var word = document.getElementById('wordChoice').value; 

function flipChk(){

if(word.length< 2){
console.log('yes');

} else {
console.log('no');




$ b

请参阅完整实现的代码: http://supsean.com/supsean/flipr/flipr.html

解决方案

您是否尝试过调试代码并查看单击按钮时 word 的值?



我猜你的问题的一部分是,当页面加载时,你将一个值赋给 word 但当用户点击按钮时不会再读取它。


I'm a total n00b to JavaScript and fully expect this is something appallingly obvious but I cannot understand why this if ( ) statement is not producing the correct results.

No matter how many characters I input the function returns 'yes' to the console whether 'word' is < or > 2. IF (ha!) anyone can shed any light on this it's greatly appreciated. Code below:

var word = document.getElementById('wordChoice').value;  

function flipChk () {

if (word.length < 2) {
    console.log('yes');

} else { 
    console.log('no');
}
}

See the fully implemented code here: http://supsean.com/supsean/flipr/flipr.html

解决方案

Have you tried debugging your code and seeing what the value of word is when you click the button?

I'm guessing part of your problem is that you're assigning a value to word when the page loads but not reading it again when the user clicks the button.

这篇关于IF()语句令人费解的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆