最后一个 If 语句被忽略

Last If statement ignored

我最后的 if 语句在执行时被忽略了。

尝试调试时,我将 console.log 添加到 if 语句中,它按预期运行。

$(document).ready(function() {  
    // set HTML
    var myVar = 'Null';
    if ( document.location.href.indexOf('?1501aa') > -1 ) {
        myVar = '01';
    }
    if ( document.location.href.indexOf('?1501ab') > -1 ) {
        myVar = '02';
    }
    if ( document.location.href.indexOf('?1501ac') > -1 ) {
        myVar = '03';
    }
    if ( document.location.href.indexOf('?1501ad') > -1 ) {
        myVar = '04';
    }
    if ( document.location.href.indexOf('?1501ae') > -1 ) {
        myVar = '05';
    }
    if ( document.location.href.indexOf('?1501af') > -1 ) {
        myVar = '06';
    }
    if ( document.location.href.indexOf('?1501ag') > -1 ) {
        myVar = '07';
    }
    if ( document.location.href.indexOf('?1501ah') > -1 ) {
        myVar = '08';
        //console.log('myVar set to' + myVar);
    }
    if ( document.location.href.indexOf('?1501aj') > -1 ) {
        myVar = '09';
        //console.log('myVar set to' + myVar);
    }
    else {
        setText();
    }
    function setText() {
        $('<label for="name">' + myVar + '</label>').insertBefore($('#name'));
    }
});

由于处理 URL 的方式,jsFiddle example 并不完美。

如何让最后一个 if 语句 return 为真并执行?

你的 if 链很奇怪。我会使用 else if,然后最后的 else 将应用于所有 if 语句。现在你的 else 只适用于最后一个 if 块。

if ( document.location.href.indexOf('?1501aa') > -1 ) {
        myVar = '01';
    }
    else if ( document.location.href.indexOf('?1501ab') > -1 ) {
        myVar = '02';
    }
    else if ( document.location.href.indexOf('?1501ac') > -1 ) {
        myVar = '03';
    }
    else if ( document.location.href.indexOf('?1501ad') > -1 ) {
        myVar = '04';
    }
    else if ( document.location.href.indexOf('?1501ae') > -1 ) {
        myVar = '05';
    }
    else if ( document.location.href.indexOf('?1501af') > -1 ) {
        myVar = '06';
    }
    else if ( document.location.href.indexOf('?1501ag') > -1 ) {
        myVar = '07';
    }
    else if ( document.location.href.indexOf('?1501ah') > -1 ) {
        myVar = '08';
        //console.log('myVar set to' + myVar);
    }
    else if ( document.location.href.indexOf('?1501aj') > -1 ) {
        myVar = '09';
        //console.log('myVar set to' + myVar);
    } else {
    //code what else you want to happen
}
setText();

从 if 结构的条件(else)部分中取出 setText() 执行;它不是 运行 除非不是值是 09,这使得它看起来像 09 不工作...

if ( document.location.href.indexOf('?1501aj') > -1 ) {
    myVar = '09';
    //console.log('myVar set to' + myVar);
}
setText();
Wait what are we doing? I dunno here is this.  

    var myVar = "";

    fucker("02","ab");

    fucker("03","ac");

    //and so on

    $('<label for="name">' + myVar + '</label>').insertBefore($('#name'));

    function fucker(num,word) {
        if(document.location.href.indexOf("?1501"+word) > -1) {
            myVar = num;
        } 
        return;
    }

原来的问题rfornal已经解释过了,但是我想我应该提一下这段代码确实需要DRYed所以没有那么多重复的代码。有几种方法可以做到这一点。

正在根据找到的字符计算 myVar 结果:

$(document).ready(function() {  
    var myVar = 'Null';
    var match = window.location.search.match(/1501a(?)/);
    if (match) {
        var chr = match[1].charAt(0);
        if (chr >= 'a' && chr <= 'h') {
            var code = match[1].charCodeAt(0) - 'a'.charCodeAt(0) + 1;
            myVar = '0' + code;
        } else if (char === 'j') {
            myVar = '09';
        }
    }
    $('<label for="name">' + myVar + '</label>').insertBefore($('#name'));
});

而且,这是使用查找的另一种方式 table:

$(document).ready(function() {  
    var myVar = 'Null';
    var matches = {'1501aa': '01', '1501ab': '02', '1501ac': '03', '1501ad': '04', '1501ae': '05', 
       '1501af': '06', '1501ag': '07', '1501ah': '08', '1501aj': '09'};
    var match = window.location.search.match(/1501a?/);
    if (match) {
        var result = matches[match[0]];
        if (result) {
            myVar = result;
        }
    }
    $('<label for="name">' + myVar + '</label>').insertBefore($('#name'));
});

此外,还有这些:

  1. 使用window.location.search
  2. 去掉setText()函数,因为不需要,单行代码直接执行即可
  3. 使用正则表达式查找匹配项

这是使用 json 和循环的完全不同的迭代。

//Function for setting the label
function setText(myVar) {
    $('<label for="name">' + myVar + '</label>').insertBefore($('#name'));
}

//used as the document.location.href
var url = "http://test.test.com?1501ad";
var myVar; //create the var but don't initialize it *note that creating myVar = 'Null'* does not create a null object. Though you don't need this. just overload the setText() 

//create an array of values as a json object
var urls = [{"url": "?1501ab", "myVar": "01"},
            {"url": "?1501ac", "myVar": "02"},
            {"url": "?1501ad", "myVar": "03"},
            {"url": "?1501ae", "myVar": "04"},
            {"url": "?1501af", "myVar": "05"},
            {"url": "?1501ag", "myVar": "06"},
            {"url": "?1501ah", "myVar": "07"},
           ];

//loop through and only set text when it finds the proper url ending test by adjusting the url variable.          
$.each(urls, function(key, value){
    if(url.indexOf(value.url) > -1){
        setText(value.myVar);
    } 
});

http://jsfiddle.net/538t9bfk/