将值附加到动态id


append value to a dynamic id

在我的PHP代码中,我有一个标签,ID是数据库中行的ID

<h1 id="totalValue'.$row['xID'].'">'."$row[xValue]".'</h1>

我有一个锚标记,当单击它时,它将通过onclick javascript函数increment(this.id) 发送$row['xID']

现在我面临的问题是,当值被发送到php文件以在数据库中递增并返回时,我似乎无法将响应ID(即上面的<h1>)与动态ID作为目标。

有办法做到这一点吗?谢谢

这是javascript函数:

function increment(id) {
    if (ajax) {
        ajax.open('get', 'update.php?xv=' + encodeURIComponent(id));
        ajax.onreadystatechange = handle_xvalue;
        ajax.send(null);
    }
    else {
        // can't use ajax
    }
} 
// hanlde response from php script
function handle_xvalue() {
    // if everything ok
    if ( (ajax.readyState == 4) && (ajax.status == 200) ) {
        document.getElementById('totalValue'+id).innerHTML = ajax.responseText;
    }
} 

这是onclick事件:

<a href="#" id="'.$row['xID'].'" name="xvalue" onclick="increment(this.id);"><span>+1</span></a>

函数handle_xvalue()似乎没有从任何地方获取该ID。

您可以尝试将该ID传递给函数

function handle_xvalue(id)
{
 //do stuff
}