dongmi1864 2014-02-16 06:32
浏览 31
已采纳

Onchange弹出错误javascript

I try to auto calculate the input without any button. I try to do it with javascript onchange function.However, javascript keep pop out the error and I do not know how to solve it.

This is the error : Uncaught SyntaxError: Unexpected identifier

below is my code :

<!DOCTYPE html>
<html>

<script>
function jsfunction()
{
var x=document.getElementById("qty1");
var y =document.getElementById("price1");
var z =document.getElementById("total1");


   alert("You entered: " + y.value);


}
</script>

<body>
<?php
$item=array("A"=>"10","B"=>"11","C"=>"12","D"=>"13");
$var=1;
$total=0;
$gtotal=0;

?>
<table border=1 width='600'
<tr>
<th>No</th>
<th>item</th>
<th>Check</th>
<th>Price</th>
<th>Qty</th>
<th>Total</th>
</tr>";
<?php
foreach($item as $myItem=>$pricetag){
echo "<tr>";
echo "<td>".$var."</td>";
echo "<td>".$myItem."</td>";
$price = "price". $var;
$qty = "qty". $var;
$total = "total". $var;
?>

<td><input type="checkbox" id="chkbox" name="chkbox"/></td>
<td><input type="text" name=<?php $price ?> id=<?php $price ?>  size =1/><?php  echo $price ?> </td>
<td><input type="text" onchange="jsfunction() name=<?php $qty ?> id=<?php $qty ?> size =1/><?php echo $qty ?></td>
<td><input type="text" name=<?php $total ?> id=<?php $total ?> size =1/></td>


<?php
$var++;
}?>


</table>
</form>
</body>
</html>
  • 写回答

3条回答 默认 最新

  • doutang7707 2014-02-16 06:40
    关注

    Well, there are a few problems:

    1. You never have the closing > of the <table> tag:

      <table border=1 width='600' => <table border=1 width='600'>

    2. You have a random "; when the code is not even PHP nor JavaScript:

      <th>Total</th>
      </tr>";
      
    3. You are using the PHP tag as an echo tag:

      <?php $price ?> => <?php echo $price ?>

    4. You never close the double quotes when calling your JavaScript function:

      onchange="jsfunction() => onchange="jsfunction()"

    5. EDIT:
      If your name property is NULL, it may cause problems because the id will be set to the name, so you should wrap the name in quotes (Just to be safe you should probably do it with everything including id, size, name, etc.):

      name=<?php $price ?> => name="<?php $price ?>"

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集