0

正如标题所说,我正在尝试将一些文本附加到 html 中的 textarea,但结果总是有一些涉及 NaN 的奇怪文本出现在 textarea 中......这是我的代码:

  <!DOCTYPE html>
  <html>
  <head>
  </head>
  <body>
  <?php 

     $_SESSION['currentlocation'] = "combat.php"; 
     ?>
     <script type="text/javascript">
            if(document.getElementById("wrapper") == null) {
              window.location = "../index.php";    
           }
       </script>

<textarea id="combatinfo" rows="4" cols="50"></textarea>
<br />   
<?php
$_SESSION['currentlocation'] = "combat.php";

if($_SESSION['ambush'] && $_SESSION['flee']) {

    for($i = 0; $i < sizeOf($_SESSION['enemies']); $i++) {
        echo $_SESSION['enemies'][$i]->rank;
        $scaling = 20 - $_SESSION['enemies'][$i] -> luck;
        if($_SESSION['enemies'][$i]-> rank >= 20 && $_SESSION['enemies'][$i]-> rank < 40) {

            $prob = rand(1, 100);

            if($prob <= 100) {              
       //                if($prob <= 75 - $scaling * 3.8) {
                if($_SESSION['comboAttack'] < 2) {
                    $_SESSION['comboAttack'] = 2;
                }

                $_SESSION['enemies'][$i]->comboAttack = 2;
              ?>                  
                <script type="text/javascript">
                    $(document).ready(function() {


                         $('#combatinfo').append("HELLO WORLD!");

                    });
                </script>    
        <?php        
            }

        }
        else if($_SESSION['enemies'][i]-> rank >= 40 && $_SESSION['enemies'][i]-> rank < 60) {
            $prob = rand(1, 100);

            if($prob <= 50 - $scaling * 3.8) {
                if($_SESSION['comboAttack'] < 3) {
                    $_SESSION['comboAttack'] = 3;
                }

                $_SESSION['enemies'][i]->comboAttack = 3;
               ?>                    
                <script type="text/javascript">
                    $(document).ready(function() {

                        var str1 = "Enemy ";
                        var str2 = "pulls off a 3-hit combo.\n";
                        var str = str1.concat(str2);

                        $('#combatinfo').append("HELLO WORLD!");
                    });
                </script>
           <?php
            }
            else if($prob <= 75 - $scaling * 3.8) {
                if($_SESSION['comboAttack'] < 2) {
                    $_SESSION['comboAttack'] = 2;
                }

                $_SESSION['enemies'][i]->comboAttack = 2;
           ?>
                <script type="text/javascript">
                    $(document).ready(function() {  
                        var str1 = "Enemy ";
                        var str2 = "pulls off a 2-hit combo.\n";
                        var str = str1.concat(str2);
                        $('#combatinfo').append("HELLO WORLD!");
                    });
                </script>
     <?php                        
            }
        }

        $_SESSION['enemies'][$i] ->attack($i);
    }
 ?>
<button type="button">Defend</button> <button type="button">Flee</button>
<form action="index.php" method="post">
<input type="submit" name="submit" value="Submit" />
</form>
<?php           
}
else if(ambush && !flee) {
?>        
<button type="button">Defend</button> <button type="button">Flee</button> 
<?php
}
if(!ambush && flee) {
?>        
<button type="button">Attack</button> <button type="button">Flee</button>
<?php
}
else if(!ambush && !flee) {
?>        
<button type="button">Defend</button> <button type="button">Flee</button>
<?php     
}
?>
<br />
<select id="selectClone">        
<?php
if(sizeOf($_SESSION['playercharacter'] -> clones) == 0) {
?>
    <script type="text/javascript">
        $(document).ready(function() {
            var dropDownMenu = document.getElementById("selectClone");
            dropDownMenu.disabled = true;
        });
    </script>             
    <option value="Player">Player</option>
<?php        
}
else if(sizeOf($_SESSION['playercharacter'] -> clones) == 1) {
?>
    <option value="Clone 1">Clone 1</option>              
<?php
}
?>            
</select>

<div id="player" class="group">asdf</div>
<div id="clone1" class="group">dsfdsfds</div>    

<script type="text/javascript">
    $(document).ready(function() {
        $('.group').hide();
        $('#player').show();
        $('#selectClone').change(function () {
            $('.group').hide();
            $('#'+$(this).val()).show();
        })
    });
</script>

出于某种原因,在文本区域中,我将文本“Enemy #NaN”附加到我想要的文本上。为什么会发生这种情况,我该如何阻止它发生?

4

1 回答 1

1

NaN是 Javascript(和其他语言)中的一个特殊值。它的意思是“不是一个数字”。

NaN当您尝试对非数字值进行数字运算时,您会得到。例如,在两个字符串之间进行除法 - 例如:

console.log("foo" / "bar");
> NaN

或一个常见的,parseInt()在一个非数字的变量上:

parseInt("foo");
> NaN

如果您随后尝试显示该值,您将获得NaN正在显示的文本。

NaN本身是一个特殊的值,类似于nullor undefined,但对 Javascript 语言有自己的含义。

在您的问题中混合使用 PHP 和 JS 代码,很难阅读和找到问题,但我相信有了上述信息,您自己应该更容易解决。

于 2013-06-17T20:17:37.743 回答