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 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值