忽略最后一个 If 语句

Last If statement ignored

本文关键字:语句 If 最后一个      更新时间:2023-09-26

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

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

$(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'));
    }
});

jsFiddle 示例并不完美,因为它们处理 URL 的方式。

如何让最后一个 if 语句返回 true 并执行?

setText()执行从 if 结构的条件(else)部分中删除;除非不是值为 09,否则它不会运行,这使得它看起来像 09 不起作用......

if ( document.location.href.indexOf('?1501aj') > -1 ) {
    myVar = '09';
    //console.log('myVar set to' + myVar);
}
setText();

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

根据找到的字符计算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'));
});

而且,这是使用查找表的不同方法:

$(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. 使用正则表达式查找匹配项
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;
    }

这是一个使用 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/

你的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();