我正在使用 PHP、AJAX 和 jQuery,所以当这个 if 语句处理时,它将显示所有已被选中的玩家,同时放置一个删除按钮和一个 onclick="removeplayer(test);"
。但是,当我单击“删除播放器”按钮时,我将得到一个 null
或 undefined
。
PHP
if(count($_SESSION['players'],COUNT_RECURSIVE) == $_SESSION['savedPlayers'] )
{
foreach($_SESSION['players'] as $key => $value){
echo "Player #: . $key . ; Player Name: . $value . \n";
echo "<input type=\"button\" name=\"" . $key . " \" id=\"" . $value . " \" value=\"Remove\" onclick=\"removePlayer(" . $value . ");\"> <br />";
}
echo "Max number of Players";
}
我的 JavaScript 现在是基本的,因为我只是想确保我正确传递了 id。我最终会添加 jQuery 来从列表中删除播放器。
这是我的 JavaScript:
function removePlayer(test){
alert(test);
};
最佳答案
这是因为您没有将 $value 括在引号中试试这个
echo "<input type=\"button\" name=\"" . $key . " \" id=\"" . $value . " \" value=\"Remove\" onclick=\"removePlayer('" . $value . "');\"> <br />";
关于php - getElementId 返回 null 或 undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7164712/