简体   繁体   English

JavaScript变量外部函数未保留值

[英]JavaScript variable out side function not retaining the value

I have an issue with the code below, I'm using onclick attribute to call the function below, then checking for which type of data-attr is passed. 我的下面的代码有问题,我在使用onclick属性调用下面的函数,然后检查要传递的数据类型。 then assigning it to a local variable outside the function. 然后将其分配给函数外部的局部变量。 but some how it saves 1 attr and displays it on alert and show the other one as undefined and then when I click on the other attr it shows it and for the first one says undefined. 但是有些方法如何保存1个attr并在警报中显示它,并将另一个显示为undefined,然后当我单击另一个attr时显示它,第一个显示为undefined。 my js and HTML below. 我的js和HTML如下。

jQuery code: jQuery代码:

<script>
var attr1 = null;
var attr2 = null;


function createLink(sportAttr) {

    if($(sportAttr).attr("data-provider-sport-id") != typeof undefined && $(sportAttr).attr("data-provider-sport-id") !== false) {

        attr2 = $(sportAttr).attr("data-provider-sport-id");

        //alert(attr2);
    }

    if($(sportAttr).attr("data-main-sport-id") != typeof undefined && $(sportAttr).attr("data-main-sport-id") !== false) {
        attr1 = $(sportAttr).attr("data-main-sport-id");
        //alert(attr1);
    }

    alert(attr1 + " and second attr is " + attr2);
    //it never reaches the next line because for some reason one of the attrs is always null
    if(attr1 != null && attr2 != null) {

        if(confirm("Are you sure you want to create this link")) {

        }

    }

}
</script>

HTML code: HTML代码:

<i data-provider-sport-id="9" onclick="createLink(this)">Music</i>

<b data-main-sport-id="17" onclick="createLink(this)">Music</b>

when I click on the first one it displays "undefined and second attr is 9". 当我单击第一个时,它显示“未定义,第二个attr为9”。

when I click on the second one it displays "17 and second attr is undefined" 当我单击第二个按钮时,它显示“ 17,第二个属性未定义”

any clues? 有什么线索吗?

This check is incorrect: 此检查不正确:

$(sportAttr).attr("data-main-sport-id") != typeof undefined

See, typeof undefined is always the string 'undefined' (as typeof always returns a string). 请参阅, typeof undefined始终是字符串 'undefined' (因为typeof始终返回字符串)。 This statement will be true unless the data-attribute happens to be the same - ie string 'undefined' . 除非数据属性碰巧相同(即字符串'undefined'否则此语句将为true。

So your problem is not retention, but the fact that you overwrite the values due to misconfigured if statements. 因此,您的问题不是保留,而是由于if语句配置错误而覆盖值的事实。

To fix this, use this undefined check (which you probably tried in the first place) : 要解决此问题,请使用以下未定义的检查(您可能首先尝试过此检查

typeof $(sportAttr).attr("data-main-sport-id") !== 'undefined'

Or just use plain comparison with undefined, without typeof (but this is less robust, so your intuition to use typeof was not bad, per se.) : 或者只使用未定义的普通比较,不带typeof (但这不够稳健,因此使用typeof的直觉本身并不坏)

$(sportAttr).attr("data-main-sport-id") !== undefined

Instead of checking if the attribute is not undefined and value is not false, you can check only the Truthy value as below - 您可以仅检查Truthy值,而不是检查属性是否未定义且值不为false,如下所示:

 var attr1 = null; var attr2 = null; function createLink(sportAttr) { if($(sportAttr).attr("data-provider-sport-id")) { attr2 = $(sportAttr).attr("data-provider-sport-id"); //alert(attr2); } if($(sportAttr).attr("data-main-sport-id")) { attr1 = $(sportAttr).attr("data-main-sport-id"); //alert(attr1); } alert(attr1 + " and second attr is " + attr2); //it never reaches the next line because for some reason one of the attrs is always null if(attr1 != null && attr2 != null) { if(confirm("Are you sure you want to create this link")) { } } } 

Hope this helps! 希望这可以帮助!

First I recommend to you avoid to set the onclick on your html, it's not a good practice. 首先,我建议您避免在HTML上设置onclick ,这不是一个好习惯。 Also I have improved your code, take a look below: 另外,我还改善了您的代码,请看以下内容:

JS: JS:

$(document).ready(function(){
    $('.myAction').on('click', function(){
        createLink($(this).data('id'))
    });

});

function createLink(sportAttr) {
    if(typeof sportAttr !== 'undefined') {
        alert(sportAttr);
    }

}

HTML: HTML:

<i class="myAction" data-id="9" data-provider-sport-id="9">Music</i>

<b class="myAction" data-id="17" data-main-sport-id="17">Music</b>

Basically the idea is make a generic function and try to reduce the code and make it more clean, also you were not checking properly the undefined . 基本上,这个想法是使一个泛型函数并尝试减少代码并使其更简洁,同时您也没有正确检查undefined

if(typeof sportAttr !== 'undefined') {
        alert(sportAttr);
    }

DEMO 演示

I hope it's helps. 希望对您有所帮助。

删除typeof运算符, typeof undefined"undefined"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM