Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 最后一个If语句被忽略_Javascript_Jquery_Dom - Fatal编程技术网

Javascript 最后一个If语句被忽略

Javascript 最后一个If语句被忽略,javascript,jquery,dom,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.

我的最后一个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的方式不同,所以它并不完美


如何使最后一条if语句返回true并执行?

您的if链很奇怪。我将使用elseif,然后最后的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();
将setText执行从if结构的条件else部分中取出;除非not值为09,否则它不会运行,这使它看起来像09不工作

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

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

根据找到的字符计算myVar结果: 下面是使用查找表的另一种方法: 此外,这些还包括:

使用window.location.search 去掉setText函数,因为它不是必需的,所以可以直接执行一行代码 使用正则表达式查找匹配项
这里是一个完全不同的使用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);
    } 
});

ot:这是否意味着最后一个ifs值以j而不是i结尾?对不起,但还不清楚您的问题是什么。如果提供了propery indexOf,那么最后一个if语句似乎工作正常。但是,如果最后一个if语句为true,则由于else而不会调用setText。您不希望执行setText而不考虑myVar值吗@showdev如果你发布了一个答案,我会选择它作为正确答案。看起来你需要window.location.search和一个对象,而不是那堆代码。除了setText是使用myVar值的地方,所以我很怀疑这是OP的意图。setText使用myVar,那么为什么它要在else中开始?为什么标记它不好?我正在使用提供的代码。我只是说用else-if代替All-if语句。这在逻辑上是有意义的,而且是一个更清晰的用法。为了清晰起见,因为你们不能阅读。我是说else只在最后一个if街区开火。因此,如果它符合沿途的任何其他条件,最后一个条件仍然会触发。我确信这不是故意的。在您的代码中,除非myVar为NULL,否则不会调用setText。我相信这也不是故意的。非常好,但我不认为有一种可以说更好的替代方法可以帮助OP或未来的读者理解这个问题。也许你可以添加一些解释?谢谢这个解决方案。虽然我同意@showdev的说法,这并不能帮助我理解这个问题,但我总是希望看到一个更好的解决方案。我不知道这家伙到底想要什么。哈哈,我明白了,但我不知道。我能告诉OP的是,在做一些JQuery的时候要更高效,冷静下来。这家伙在最后一个if-only上有一个if-else。对除最后一个之外的所有选项使用IF-ELSE。如果当前操作代码不等于解释的最后一个indexOfThanks@Teemu,那么它将始终激发else!您的答案中没有JSON:。
$(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'));
});
//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);
    } 
});