-1

我正在尝试将两个文本框中的值相乘并将其放在第三个文本框中...使用 javascript 和 php while 循环与 onclick 事件.....怎么做?我的代码是

<script language="javascript">

    function multiply()
    {

       var textValue1 = document.getElementById('CLS').value;
       var textValue2 = document.getElementById('rate').value;

       document.getElementById('valuation').value = textValue1 * textValue2;

    }
    </script>
<?php
$a="some query";
$b=mysql_query($a);
while($c=mysql_fetch_array($b))

{
echo "<td>".$CLOSINGstk."</td>
<input type='hidden' name='CLOSINGstk".$a."' value='".$CLOSINGstk."' id='CLS".$a."'>";
echo "<td><input type='text' name='rate".$a."' id='rate".$a."'></td>";
echo "<td><input type='text' name='valuation".$a."' id='valuation".$a."' onclick='multiply();'></td>";
}
?>
4

2 回答 2

1

像这样试试

while($c=mysql_fetch_array($b)) {
echo "<td>
          <input type='text' name='valuation".$c['id']."'
           id='valuation".$c['id']."' onkeypress='multiply(".$c['id'].");'>  //Let you want to pass id from DB
      </td>";

或者你可以使用

onkeypress='multiply('.$c['id'].')'

在你的脚本中使用onkeypress而不是onclick ()

function multiply(id)
{

   var textValue1 = document.getElementById('CLS'+id).value;
   var textValue2 = document.getElementById('rate'+id).value;

   document.getElementById('valuation'+id).value = textValue1 * textValue2;

}

并尽量避免使用 mysql_* 函数,因为它们已被贬低,而是使用mysqli_*函数或PDO语句

于 2013-06-05T05:48:34.353 回答
1
<script language="javascript">

    function multiply(arg_id)
    {

       var textValue1 = document.getElementById('CLS'+arg_id).value;
       var textValue2 = document.getElementById('rate'+arg_id).value;

       document.getElementById('valuation'+arg_id).value = textValue1 * textValue2;

    }
    </script>
<?php
$a="some query";
$b=mysql_query($a);
while($c=mysql_fetch_array($b))

{
echo "<td>".$CLOSINGstk."</td>
<input type='hidden' name='CLOSINGstk".$a."' value='".$CLOSINGstk."' id='CLS".$a."'>";
echo "<td><input type='text' name='rate".$a."' id='rate".$a."'></td>";
echo "<td><input type='text' name='valuation".$a."' id='valuation".$a."' onclick='multiply(".$a.");'></td>";
}

?>

只需将参数作为 id 传递给所有CLS,RATE,VALUATION

让我知道我可以帮助你更多。

于 2013-06-05T05:52:24.673 回答