dtl4521 2015-08-27 10:11
浏览 46
已采纳

Js,php,onclick函数和引号错误

I'm setting up a fantasy soccer website and I'm having troubles with quotes and javascript.

I have one table of player names and a set of 18 <div> (11 starting players and 7 bench ones) on a soccer field image. I want the user to be able to click on a name on the table, and the name simultaneously appears on the field and disappears from the table. The player name must appear at the first "empty" position (either starting or bench). On the other side, if the user click on a <div>, the name must disappear from the field/bench and appear in the table at the original position.

It's way more easy to see than to explain! :)

I was able to do that but I'm getting mad once I found out there were players with ' in their names...

I have the following piece of code with an array for taking care of the 18 position (var team) and two functions

<script>
    team = new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0);

    function insertPlayer(name)
    {
        var j = 0;
        for (j=0; j<18; j++) {
            if(team[j] == 0){
                break;
            }
        };
        if(j<18){
            team[j] = 1;
            var num = j.toString();
            var id_string = 'player_'.concat(num);
            document.getElementById(j).textContent = name;
            document.getElementById(id_string).value = name;
            document.getElementById(name).style.display = 'none';
        }
    }

    function removePlayer(id)
    {
        team[id] = 0;
        var num = id.toString();
        var id_string = 'player_'.concat(num);
        document.getElementById(document.getElementById(id).textContent).style.display = "";
        document.getElementById(id).textContent = "";
        document.getElementById(id_string).value = "";
    }
</script>

The <div> are like this one

<?php
    for ($i=0; $i < 18; $i++) { 
      echo '<div onclick="removePlayer(\''.$i.'\')" id="'.$i.'">';
      if(isset($array[$i])){echo $array[$i];}
      echo '<input type="hidden" id="player_'.$i.'" name="player_'.$i.'" value=';
      if(isset($array[$i])){echo '"'.$array[$i].'"';}
      echo '></div>';
    }
?>

where, of course, $array[$i] is the saved lineup, if the user has already done it and he wants to modify his first choice. The table is like this one:

<tr id="'.mysqli_real_escape_string($link,$name).'">
...
<td>...</td>
<td onclick=\"insertPlayer('".mysqli_real_escape_string($link,$name)."')\">".$name."</td>
<td>...</td>
...

As I said, this works great with normal names. It gives me troubles with name like "O'Neal" for example...

Thanks for your help!

  • 写回答

1条回答 默认 最新

  • duanmen8491 2015-08-27 10:22
    关注

    Put the player names into a Javascript array, and then use the array index in the function call.

    <script>
        team = new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0);
        var players = <?php echo json_encode($array); ?>;
    
        function insertPlayer(playernum)
        {
            var name = players[playernum];
            var j = 0;
            for (j=0; j<18; j++) {
                if(team[j] == 0){
                    break;
                }
            };
            if(j<18){
                team[j] = 1;
                var num = j.toString();
                var id_string = 'player_'.concat(num);
                document.getElementById(j).textContent = name;
                document.getElementById(id_string).value = name;
                document.getElementById(name).style.display = 'none';
            }
        }
    
        function removePlayer(id)
        {
            team[id] = 0;
            var num = id.toString();
            var id_string = 'player_'.concat(num);
            document.getElementById(document.getElementById(id).textContent).style.display = "";
            document.getElementById(id).textContent = "";
            document.getElementById(id_string).value = "";
        }
    </script>
    
    ...
    
    <tr id="player_insert_'.$i.'">
    ...
    <td>...</td>
    <td onclick=\"insertPlayer('.$i.')\">".$name."</td>
    <td>...</td>
    ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 镍氢电池充电器设计实物使用原理
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号