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 MATLAB yalmip 可转移负荷的简单建模出错,如何解决?
  • ¥15 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?