Javascript 使用ajax和jquery向数据库添加播放计数

Javascript 使用ajax和jquery向数据库添加播放计数,javascript,jquery,ajax,Javascript,Jquery,Ajax,当用户单击.play时,如果没有类alreadyplayed,它应该添加一个播放计数,然后添加类alreadyplayed我是AJAX新手,似乎无法让它工作 $(".play").on('click', function () { if (!$(this).hasClass('alreadyplayed')) { $.ajax({ type:'post', url:'addplay.php', data: songid = $(this).attr('sound

当用户单击.play时,如果没有类alreadyplayed,它应该添加一个播放计数,然后添加类alreadyplayed我是AJAX新手,似乎无法让它工作

$(".play").on('click', function () { 
if (!$(this).hasClass('alreadyplayed')) {
    $.ajax({
    type:'post',
    url:'addplay.php',
    data: songid = $(this).attr('sound_id'),
    }).done {
    $(this).addClass('alreadyplayed');
    }
}); 

问题似乎在于
数据:songid=$(this).attr('sound_id'),
完成
函数

您必须将其作为对象传递

data: { songid : $(this).attr('sound_id')} // Note curly braces
.done是一个回调函数

.done(function(response){
 //rest of code
}
你可以试试这个片段

 $(".play").on('click', function (event) { 
if (!$(this).hasClass('alreadyplayed')) {
    $.ajax({
    type:'post',
    url:'addplay.php',
    data: {songid = $(this).attr('sound_id')},
    }).done(function(response){ //End of ajax & start of done
        $(this).addClass('alreadyplayed');
       }) // end of done
    } // end of if loop
}); // end of click

问题似乎在于
数据:songid=$(this).attr('sound_id'),
完成
函数

您必须将其作为对象传递

data: { songid : $(this).attr('sound_id')} // Note curly braces
.done是一个回调函数

.done(function(response){
 //rest of code
}
你可以试试这个片段

 $(".play").on('click', function (event) { 
if (!$(this).hasClass('alreadyplayed')) {
    $.ajax({
    type:'post',
    url:'addplay.php',
    data: {songid = $(this).attr('sound_id')},
    }).done(function(response){ //End of ajax & start of done
        $(this).addClass('alreadyplayed');
       }) // end of done
    } // end of if loop
}); // end of click

我是这样做的,最终当我弄明白的时候,我会让它成为post data v.s.?songid=get方法,我会加上它,只是为了让知道addplay页面的人不能让一个播放助推器变得那么容易,但现在它工作得很好

 $(".play").on('click', function () { 
    var soundid = $(this).attr('soundid');
    var thisplay = $(this);
        if (!$(this).hasClass('alreadyplayed')) {
                $.ajax({
                type:'post',
                data: $(this),
                url:'addplay.php?songid=' + soundid,        
                   });   
            }
            thisplay.addClass('alreadyplayed');
});

我是这样做的,最终当我弄明白的时候,我会让它成为post data v.s.?songid=get方法,我会加上它,只是为了让知道addplay页面的人不能让一个播放助推器变得那么容易,但现在它工作得很好

 $(".play").on('click', function () { 
    var soundid = $(this).attr('soundid');
    var thisplay = $(this);
        if (!$(this).hasClass('alreadyplayed')) {
                $.ajax({
                type:'post',
                data: $(this),
                url:'addplay.php?songid=' + soundid,        
                   });   
            }
            thisplay.addClass('alreadyplayed');
});