jQuery Knob Release function additional parameter

2019-09-03 04:28发布

问题:

I'm using the jQuery Knob plugin https://github.com/aterrien/jQuery-Knob

Now, I've got the following jQuery Knob Initialization

loop
   <input type="text" value="<?php echo $score; ?>" class="circle-rating" data-entryid="<?php the_ID(); ?>">
endloop



$(function() {
    $(".circle-rating").knob({
            'min':0,
            'max':10,
            'step':1,
            'width':40,
            'height':40,
            'fgColor':"#F59B00",
            'inputColor':"#F59B00",
            'displayPrevious': true,
            'release' : function (v) { 

                var entry_id = $(this).attr('data-entryid');

                jQuery.post("/path/to/file/update_library_score.php", {v : v, entry_id : entry_id}, function(data) {
                    console.log(entry_id);
                    jQuery('#notification-general').html(entry_id);

                });
            }

    });
});

The main issue is that I have multiple knobs on the page. These knobs are actually in a loop with wordpress posts.

Anyway, each knob is attached to an ID and this ID changes as you go through the loop.

Now to be able to update the score I need two things the knob's value which I get from the release function and I also need the post_id which I can only get within the loop. So how can I get the post_id variable to this function?

Usually I can simply add a button or a link with a onclick="my_function(<?php echo $post_id; ?>) however, I can't do it with this. What's the best way of grabbing the $id that corresponds with this knob and passing it to the release function as a parameter?

回答1:

Try this in release function

alert(this.$.attr('data-entryid'));

See demo here