【问题标题】:jquery get global values from functionsjquery 从函数中获取全局值
【发布时间】:2011-07-08 13:12:16
【问题描述】:

您好,我有一个问题,想在 jQuery、AJAX 和 jQuery UI 中创建金额计算器。但我有一个问题,我从滑块、选择框中获取所有重要值。巴德我真的不知道如何才能将这个价值推向全球。我只在每个函数中获取值,但是当我想从函数中获取这个值时,变量中的这个值不存在。

$(function() {
    //1st slider for get amount
    $("#amount").slider({
        range: "min",
        value: 0,
        min: 0,
        max: 10000,
        slide: function (e, ui) {
            $("#amount_show").val(ui.value);
            amount = ui.value;
            }
    });
    $("#amount_show").val($("#amount").slider("value"));

    //2nd slider for get values years
    $("#years").slider({
        range: "min",
        value:1,
        min:1,
        max:20,
        step:1,
        slide: function (e, ui) {
            $("#years_show").val(ui.value);
            years = ui.value;
            }
    });
    $("#years_show").val($("#years").slider("value"));

    //AJAX for loading values from xml
    $.get("irate.xml", function(xml) {
            $(xml).find('country').each(function() {

                var select = $('#country'),
                    id = $(this).attr('id'),
                    name = $(this).find('name').text(),
                    irate = $(this).find('irate').text(),
                    currency = $(this).find('currency').text();

                select.append("<option value='"+irate+"'>"+name+"</option>");
        });
    });

    //get irate value from dropbox
    $("#country").change(function() {
        var irate_select = "";
        $("#country option:selected").each(function () {
            irate_select += $(this).val() + " ";
            });
            irate = irate_select;
        });
});

我需要用数量、年数和愤怒来计算。

感谢您的建议。

【问题讨论】:

    标签: jquery xml ajax jquery-ui


    【解决方案1】:

    在函数之外声明变量。

    例如

    var amount;
    
    $(function...
    

    【讨论】:

    • 当我在萤火虫中制作 console.log(amount) 我有“未定义”...
    • 你申报了吗?如果您想从一开始就声明它,请使用var amount = 3000000000;(这基本上就是@jbabey 所说的......)
    【解决方案2】:

    AndyL 的答案应该是正确的。

    如果您在函数之外的另一个函数中声明所需的变量,然后在一个函数中设置它们的值,则它们应该可以与这些值一起用于另一个函数。

    // outside your function!!
    var amount,
        years,
        irate;
    
    // in your function somewhere!!
    amount = someCalculation();
    years = someOtherCalculation();
    

    这些值将在所有其他函数中可用,因为它们被声明为全局命名空间的成员。

    【讨论】:

      【解决方案3】:

      尝试使用 jquery 的 data() 对象;

      $(function(){
      ...
      
      slide: function (e, ui) {
          $("#amount_show").val(ui.value);
          amount = ui.value;
      
          // Add to data() object here
          $('#amount').data('amount',amount);
      }
      
      // reference it elsewhere in your script like such:
      console.log($('#amount').data('amount'));
      

      【讨论】:

        【解决方案4】:

        一个全局变量应该可以工作,但是,如果它们改变了某些东西的值,你不应该改变你的“计算器”吗?

        假设这是计算金额的 html:

        <input type="text" id="amount" style="border:0; color:#f6931f; font-weight:bold;" />
        

        那么你将需要在 document.ready() 中类似这样的代码:

        //if the slider changes, then update the calculated value
        $( "#years,#amount" ).bind( "slidechange", function(event, ui) {
          myCalculator();
        });
        // if the ?country? drop down changes, update the calculated value
        $().change(function(event){
          myCalculator();
        });
        
        function myCalculator(){
          var years = $('#years').slider("value");
          var amount  = $('#amount').slider("value"); 
          var country = $('#country').val();
          $('#amount').val("something")
        };
        

        【讨论】:

        • 我刚刚抓住了“愤怒”的东西。你需要移动你的 'var irate_select = "";'在 #country.change() 之外,因此可以在函数 myCalculator(){} 中访问它
        猜你喜欢
        • 2018-02-19
        • 2019-06-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-31
        • 2023-01-29
        相关资源
        最近更新 更多