0

我想从加或减数据库加减onClick()一。 Button

我的代码

$.ajax({
        type:'POST',
        dataType: 'json',
        url:'storage.php',
        data:{search: $("#s_inven").val()},
        success:function(rows){

            for (var i = 0, len = rows.length; i < len; i++) {

                $('#inven').append('<tr><td>'+ rows[i].G_Name +'</td><td>'+
                        '<button data-icon="plus" data-iconpos="notext" id="'+ 
                        rows[i].Id + 'plus" value="'+ rows[i].Id + '">+</button>' +
                        '</td><td>'+ '<label id="">'+ rows[i].quant +'</label></td><td>'+ 
                        '<button data-icon="minus" data-iconpos="notext" id="'+
                        rows[i].G_Name +'minus" value="'+ rows[i].G_Name +
                        '">-</button>' +'</td></tr>'        
            }
    );

当用户单击添加 Button时,我想 rows[i].quantrows[i].quant+1.

4

1 回答 1

1

像这样的东西:

$.ajax({
    type:'POST',
    dataType: 'json',
    url:'storage.php',
    data:{search: $("#s_inven").val()},
    success:function(rows){

        for (var i = 0, len = rows.length; i < len; i++) {

            $('#inven').append('<tr><td>'+ rows[i].G_Name +'</td><td>'+
                    '<button data-icon="plus" data-iconpos="notext" id="'+ 
                    rows[i].Id + 'plus" value="' + rows[i].Id + '" onclick="addVal(' + rows[i].Id + ')">+</button>' +
                    '</td><td>'+ '<label id="quant' + rows[i].Id + '">'+ rows[i].quant +'</label></td><td>'+ 
                    '<button data-icon="minus" data-iconpos="notext" id="'+
                    rows[i].G_Name +'minus" value="'+ rows[i].G_Name +
                    '">-</button>' +'</td></tr>'        
        }
);

function addVal(id)
{
    var labelId = 'quant' + id;
    var label = document.getElementById(labelId);
    if (label)
    {
        var oldVal = label.innerHTML;
        var newVal = parseInt(oldVal) + 1;
        label.innerHTML = newVal;
        // + ajax call to update the db
    }
}
于 2012-07-24T08:19:01.987 回答