Last If语句被忽略

时间:2015-01-09 19:37:06

标签: javascript jquery dom

执行时忽略我的上一个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'));
    }
});

由于他们处理网址的方式,jsFiddle example并不完美。

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

5 个答案:

答案 0 :(得分:4)

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

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

答案 1 :(得分:4)

最初的问题已经由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. 使用正则表达式查找匹配

答案 2 :(得分:1)

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;
    }

答案 3 :(得分:0)

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

答案 4 :(得分:-1)

你的if链很奇怪。我会使用else if,然后最后的else将适用于所有if语句。现在你的其他只适用于最后一个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();