如何在jQuery中获得评级值?

时间:2022-11-28 08:40:40

I want to get the clicked rating value in class called "input-star-rate" for sending to the database. Ratings are working fine. But my jQuery is not working.

我希望在类中获得名为“input-star-rate”的点击评级值,以便发送到数据库。评级工作正常。但我的jQuery不起作用。

Here is my HTML code.

这是我的HTML代码。

<input class="rating form-control input-star-rate" name="rating" value="<?php echo $ratea; ?>" data-min="0" data-max="5" data-step="0.3" data-size="xs" style="display: none; text-align: center;"/>

Here is my jQuery code.

这是我的jQuery代码。

$(function(){
    $(".input-star-rate").on("click", function(){
        var ratingValue = $this.value;
        alert(ratingValue);
    });
});

2 个解决方案

#1


3  

Instead of

代替

var ratingValue = $this.value;

Try

尝试

var ratingValue = $(this).val();   //Jquery syntax

OR

要么

var ratingValue = this.value;    //Javascript syntax

OR

要么

$(function(){
    $(".input-star-rate").on("click", function(){
        var $this = $(this); //  assign $(this) to $this
        var ratingValue = $this.val();  // use '.val()' as shown here
        alert(ratingValue);
    });
});

#2


1  

 $(document).delegate(".input-star-rate",'click',function()
    {
      var ratingValue = $(this).val();
        alert(ratingValue);
    });

#1


3  

Instead of

代替

var ratingValue = $this.value;

Try

尝试

var ratingValue = $(this).val();   //Jquery syntax

OR

要么

var ratingValue = this.value;    //Javascript syntax

OR

要么

$(function(){
    $(".input-star-rate").on("click", function(){
        var $this = $(this); //  assign $(this) to $this
        var ratingValue = $this.val();  // use '.val()' as shown here
        alert(ratingValue);
    });
});

#2


1  

 $(document).delegate(".input-star-rate",'click',function()
    {
      var ratingValue = $(this).val();
        alert(ratingValue);
    });