使用jquery在DataBase上增加计数器

时间:2014-05-26 01:29:55

标签: javascript jquery html

我的数据库中有一个存储大量点击的字段,并且当我点击我的标签云的链接(<a href="#selecoes" data-identity="31" id="clicks" clicks="0">)时想要增加该字段。请注意,我通过我的webservice获得了点击次数。这是我到目前为止:

的index.html

<div id="tags">
      <ul id="tagList">
       <li>
           <a href="#selecoes" data-identity="31" id="clicks" clicks="0"><img src='.../>Brasil</a>
       </li>                
</ul>

main.js

    $('#tagList a').live('click', function() {
        findByIdSelecoes($(this).data('identity'));
    });

    function findByIdSelecoes(id) {
        console.log('findByIdSelecoes: ' + id);
        $.ajax({
            type: 'GET',
            url: rootURLSelecoes + '/id/' + id,
            dataType: "json",
            success: function(data){
                $('#btnDelete').show();
                console.log('findByIdSelecoes success: ' + data.nome);
                currentWine = data;
                renderDetails(currentWine);
                findJogadoresBySelecao(id);
                addClick(currentWine);

            }
        });
    }


function addClick(selecao) {
    console.log('addClick na seleção: ' + selecao.id_selecao);
    $.ajax({
        type: 'PUT',
        contentType: 'application/json',
        url: rootURLSelecoes + '/update/' + selecao.id_selecao,
        dataType: "json",
        data: formToJSON(),
        success: function(data, textStatus, jqXHR){
            alert('clicks updated successfully');


        },
        error: function(jqXHR, textStatus, errorThrown){
            alert('updateWine error: ' + textStatus);
        }
    });
}



function formToJSON() {
    return JSON.stringify({
        "clicks": ($('#clicks').val())++ // i dont know what i have to do so i try this(don't work)
        });
}

单击列表中的链接时无法更新数据库。函数formToJSON不会增加数据库中的值。

1 个答案:

答案 0 :(得分:1)

尝试这样做

将var编号设为全局,并执行以下操作:

function addClick(selecao) {
    console.log('addClick na seleção: ' + selecao.id_selecao);
    number = parseInt(selecao.clicks,10);
    $.ajax({
        type: 'PUT',
        contentType: 'application/json',
        url: rootURLSelecoes + '/update/' + selecao.id_selecao,
        dataType: "json",
        data: formToJSON(),
        success: function(data, textStatus, jqXHR){
            alert("Done: " +number);




        },
        error: function(jqXHR, textStatus, errorThrown){
            alert('updateWine error: ' + textStatus);
        }
    });
}


function`enter code here` formToJSON() {
    var ola = parseInt(number,10);
    ola = ola +1;
    return JSON.stringify({
        "clicks": parseInt(ola,10)
        });
}