ajax - 如何使用ajax检测哪个按钮被按下
问题描述
我希望每次按下“plato”按钮时(有几个,因为我在循环中创建它们)执行此函数“ $ ('# plato'). click (function ()
”但我观察到此代码仅在按下第一个按钮时执行。对于其他人,它不会被执行。
菜单.php:
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="estilos.css">
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="ajax.js"></script>
</head>
<body>
<?php
$restaurante=$_GET['restaurante'];
chdir(getcwd()."/".$restaurante);
$id_fichero= @fopen("menus.txt","r") or die("<B>El fichero no se pudo abrir");
?>
<?php
while (!feof($id_fichero))
{
if(($linea=trim(fgets($id_fichero,256),"\n\r"))!=false)
{
echo("<button type='button' class='accordion'>".$linea."</button>");
echo("<div class='panel'>");
$id_fichero2= @fopen($linea.".txt","r") or die("<B>El fichero no se pudo abrir");
while (!feof($id_fichero2))
{
if(($linea2=trim(fgets($id_fichero2,256),"\n\r"))!=false)
{
echo("<button type='button' id='plato' name='plato'>".$linea2."</button><br>");
echo("<input type='hidden' id='nombrePlato' name='nombrePlato' value='".$linea2."'>");
}
}
echo ("</div>");
}
}
?>
<div id="carrito">
</div>
<script src="collapsible.js"></script>
</body>
</html>
ajax.js
$(document).on('ready',function(){
$('#plato').click(function(){
alert("I'm here");
var url = "anadirCarrito.php";
$.ajax({
type: "POST",
url: url,
data: $('#platos').serialize(),
success: function(data)
{
$('#carrito').html(data);
}
});
});
});
解决方案
推荐阅读
- spring - 在 Spring Boot 应用程序中启用与多个 Pod 的 WebSocket 连接
- java - 试图理解 Exit、Loop、Method 以及 If 和 Else [JAVA]
- django - Kubernetes:Django 和 Postgres 容器不通信
- python - 如何从创建的函数中绘制均方误差?
- google-cloud-platform - 为什么GCP不允许VM内的外部公共IP
- node.js - Stripe TypeError:无法读取未定义的属性“payment_intent”
- anaconda - 由于硬件限制(ppc64le IBM),当我被迫使用pytorch 1.3.1时强制安装torchvision 0.4.2
- javascript - 日期之间的查询 mongo
- java - 是否可以在实例而不是流口水的类上编写规则?
- c++ - CPP OOP Code review, I cant understand why it doesnt work