奇怪的文字NaN一直出现在textarea

时间:2013-06-17 18:17:04

标签: php javascript

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

  <!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>

    

由于某种原因在textarea我得到了文字“Enemy #NaN”附加到我想要的文本上。为什么会发生这种情况,我如何阻止它发生?

1 个答案:

答案 0 :(得分:1)

NaN是Javascript(和其他语言)中的特殊值。这意味着“不是数字”。

当您尝试对非数字值进行数值运算时,将获得NaN。例如,在两个字符串之间进行划分 - 例如:

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

或常见的parseInt()对非数字变量:

parseInt("foo");
> NaN

如果您尝试显示该值,则会显示文本NaN

NaN本身是一个特殊值,类似于nullundefined,但具有Javascript语言的含义。

在您的问题中混合使用PHP和JS代码,很难阅读并发现问题,但我确信上述信息可能会让您自己更容易解决。