简体   繁体   English

从ajax请求全局设置javascript变量?

[英]set javascript variable from an ajax request globally?

I have this ajax code right here: 我在这里有这个ajax代码:

<script>
    $('a[data-id]').click(function () {
        var id = $(this).attr('data-id');
        var domain = $(this).attr('data-domain');
        $.ajax({
            url: 'getdata',
            type: 'GET',
            dataType: 'json',
            data: {id: id, domain: domain},
            success: function (data) {
                var domains = data.name + data.tld;
                var tld = data.tld;
                $('.resultdomain').html(domains);

            }
        });
    });
</script>

This code works but my problem is that I want to set the tld variable globally to use them in an if statement. 该代码有效,但我的问题是我想全局设置tld变量以在if语句中使用它们。

I want to use the variable like this in my code: 我想在我的代码中使用像这样的变量:

if(tld == .de)
{
document.write('<img src="imagelink.png" alt="denic" class="pull-right">')
}
elseif(tld == .com)
{
document.write('<img src="otherimagelink.png" alt="core" class="pull-right">')
}

but I couldn't figure out how I can set the tld variable globally to use it everywhere in my code. 但是我不知道如何全局设置tld变量以在代码中的任何地方使用它。

Thanks for any help! 谢谢你的帮助!

try defining tdl variable globally out of the scope 尝试在范围之外全局定义tdl变量

<script>
      var tdl;
      $('a[data-id]').click(function () {
        var id = $(this).attr('data-id');
        var domain = $(this).attr('data-domain');
        $.ajax({
            url: 'getdata',
            type: 'GET',
            dataType: 'json',
            data: {id: id, domain: domain},
            success: function (data) {
                var domains = data.name + data.tld;
                tld = data.tld;
                $('.resultdomain').html(domains);
                 if(tld == .de)
                 {
                    document.write('<img src="imagelink.png" alt="denic" class="pull-right">')
                 }
                 elseif(tld == .com)
                 {
                    document.write('<img src="otherimagelink.png" alt="core" class="pull-right">')
                 }
            }
        });
    });


</script>

The scope of tld variable is enclosed in your AJAX call. tld变量的范围包含在AJAX调用中。 It is only available to use within the scope of its definition. 它仅在其定义范围内可用。 I've moved it outside of your AJAX call. 我已将其移到您的AJAX通话之外。

   <script>
        var tld;
        $('a[data-id]').click(function () {
            var id = $(this).attr('data-id');
            var domain = $(this).attr('data-domain');
            $.ajax({
                url: 'getdata',
                type: 'GET',
                dataType: 'json',
                data: {id: id, domain: domain},
                success: function (data) {
                    var domains = data.name + data.tld;
                    tld = data.tld;
                    $('.resultdomain').html(domains);

                }
            });
        });
    </script>

You should check strings, not .de but ".de" 您应该检查字符串,而不是.de而是".de"

    if(tld == ".de")
    {
        document.write('<img src="imagelink.png" alt="denic" class="pull-right">')
    }
    elseif(tld == ".com")
    {
        document.write('<img src="otherimagelink.png" alt="core" class="pull-right">')
    }

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

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