如何在两个不同的jQuery事件中使用相同的变量?

时间:2020-12-12 20:55:50

I am working on a rating system and want to pull up each rating value by the jquery. For this purpose I am doing like this, but I am unable to get previous event variable value into next event.

我正在研究评级系统,并希望通过jquery提取每个评级值。为此,我这样做,但我无法将以前的事件变量值输入下一个事件。

var r1Rating;
    $(".rating-input").on("click",function(){
        //alert($(this).attr("id"));

        var r1=$(this).attr("id");
        var r1Array=r1.split("-");
        //alert(r1Array[r1Array.length-1]);
        var r1Rating=parseInt(r1Array[r1Array.length-1]).toFixed(2);
        $("#total_rating").html(r1Rating);
        var r1Rating=r1Rating;
    });

$(".rating-input1").on("click",function(){
        alert(r1Rating); //I want to get value here
});

Any help, suggestion would be appreciated. Thanks

任何帮助,建议将不胜感激。谢谢

2 个解决方案

#1


4  

Even though you have a r1Rating in the external scope, since you are using var r1Rating in the click handler, you are creating a new locally scoped variable in the click handler. So any changes you make to the variable will be visible inside that method only and the variable in the external scope will not be updated.

即使您在外部作用域中有r1Rating,因为您在单击处理程序中使用var r1Rating,您将在单击处理程序中创建一个新的本地作用域变量。因此,您对变量所做的任何更改只能在该方法中可见,并且外部范围中的变量不会更新。

var r1Rating;
$(".rating-input").on("click", function () {
    //alert($(this).attr("id"));

    var r1 = $(this).attr("id");
    var r1Array = r1.split("-");

    //should not use var here
    r1Rating = parseInt(r1Array[r1Array.length - 1]).toFixed(2);
    $("#total_rating").html(r1Rating);
});

$(".rating-input1").on("click", function () {
    alert(r1Rating); //I want to get value here
});

#2


-2  

The code below should be changes, since in this instance, you created a new r1Rating variable in a scope, which means that this was a different variable from the global one outside.

下面的代码应该是更改,因为在这种情况下,您在范围中创建了一个新的r1Rating变量,这意味着这是一个与外部全局变量不同的变量。

var r1Rating=r1Rating;

This should be changed to:

这应该改为:

r1Rating=r1Rating;

#1


4  

Even though you have a r1Rating in the external scope, since you are using var r1Rating in the click handler, you are creating a new locally scoped variable in the click handler. So any changes you make to the variable will be visible inside that method only and the variable in the external scope will not be updated.

即使您在外部作用域中有r1Rating,因为您在单击处理程序中使用var r1Rating,您将在单击处理程序中创建一个新的本地作用域变量。因此,您对变量所做的任何更改只能在该方法中可见,并且外部范围中的变量不会更新。

var r1Rating;
$(".rating-input").on("click", function () {
    //alert($(this).attr("id"));

    var r1 = $(this).attr("id");
    var r1Array = r1.split("-");

    //should not use var here
    r1Rating = parseInt(r1Array[r1Array.length - 1]).toFixed(2);
    $("#total_rating").html(r1Rating);
});

$(".rating-input1").on("click", function () {
    alert(r1Rating); //I want to get value here
});

#2


-2  

The code below should be changes, since in this instance, you created a new r1Rating variable in a scope, which means that this was a different variable from the global one outside.

下面的代码应该是更改,因为在这种情况下,您在范围中创建了一个新的r1Rating变量,这意味着这是一个与外部全局变量不同的变量。

var r1Rating=r1Rating;

This should be changed to:

这应该改为:

r1Rating=r1Rating;