简体   繁体   中英

How can I get post ID when someone play an Audio?

What I want to accomplish is, to attach POST ID to HTML audio control when playing it. I have accomplished the way to get a response when someone clicks on the play button.

Now I'm having issues to attach the post id to that js so that I can COUNT how many times an HTML audio has been played with correct data.

<div id="293">
    <audio class="6" id="mAudio" controls="">
      <source src="horse.mp3">
    </audio>

The first piece of code is my audio control rapped up with div -with POST ID

 window.onload=function(){
    var player = document.getElementById("mAudio");
    player.addEventListener("play", function () {

        console.log("audio playing");
        var post_id = $(this).data('post_id');
        var action = 'play';
        alert('Post id :' + post_id);
        $.ajax({
            url:"action.php",
            method:"POST",
            data:{post_id:post_id, action:action},
            success:function(data)
            {
               /* alert(data);*/
                fetch_post();
            }
        })
    });
}

Then this above code, as I'm trying to get post id, it says UNDEFINED

You need to go back to div who contains id. You need to do this:

 var post_id = $(this).parent().attr('id');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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