dri98076 2013-04-09 22:28
浏览 64
已采纳

无法在脚本函数中传递php变量

i am creating two text areas with id's-id1$i(when,$i=1) and id1$i(when,$i=2)..what i want is that as soon as user changes the text of any of these text areas it should display that text in an alert box.Here is my code:

<?php
$i=1;
while($i)
    {
    echo '<textarea id="id1$i" onchange=Display("id1$i")></textarea>';
    echo "id1$i..<br>";
        if($i==2){break;}
     $i=2;
     }
?>
<input name="Submit" type="submit" value="Submit"/>
<script>
function Display(id1$i)
         {
             alert(document.getElementById("id1$i").value);
             }
</script>

Now when i try running this...the problem is when i enter "one" in the first text area box,it displays "one",that's ok.....but when i enter "two" in the second text area box,it still displays "one"...:mad::mad::mad:...

The only reason dat i can think of why it's hapning lyk dat is the function Display(id1$i) is not taking the value of $i=2...please help!!..

If i use proper names like "id11" and "id12"(i.e i dont use the variable $i anywhere in my code or say if i harcode everythng)..then the code works fine..it displays both "one" "two",respectively..

  • 写回答

1条回答 默认 最新

  • duanji9378 2013-04-09 22:31
    关注

    You can't embed PHP like that. You'll need to use the <?php ?> tags, therefore you'll need to change:

    function Display(id1$i)
    // ...
    alert(document.getElementById("id1$i").value);
    

    to:

    function Display(id1<?php echo $i; ?>)
    // ...
    alert(document.getElementById("id1<?php echo $i; ?>").value);
    

    And to use variables inside strings directly you can only use " therefore you can do one of the following:

    '<textarea id="id1'. $i .'" onchange=Display("id1'. $i .'")></textarea>'
    // or
    "<textarea id=\"id1$i\" onchange=Display(\"id1$i\")></textarea>"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?