嗨,我在我的网页的大脑中使用了烧瓶,但是我在商店部分遇到了问题,每种产品都有自己添加的按钮,但是当我单击一个按钮时,它会升级所有值,但我只会t更新我单击的那个。 您能告诉我如何使用python单击哪个按钮。谢谢。 这是我的Python代码:
@app.route("/Menu/<R>", methods = ["GET", "POST"])
def menu(R):
compra = dict()
carrito = dict()
#Making sure that the user has logIn.
if "user" in session:
cursor.execute("SELECT ImagenProducto, NombreProducto, PrecioB, Stock, NombreCorto, ProductoId FROM menu inner join menulugar on IdProducto = ProductoId join listalugares on LugarId = IdLugares where Lugar = '{}' Order By IdProducto".format(R))
menu = cursor.fetchall()
for x in menu:
compra[x[4]]=0
if request.method == "POST":
for x in compra:
if x in session.keys():
compra[x] = session[x] + int(request.form.get(x))
else:
compra[x] = compra[x] + int(request.form.get(x))
for x in compra:
if compra[x]>0:
session[x]=compra[x]
for x in session:
if session[x] == session["user"]:
print('user')
else:
carrito[x] = session[x]
return render_template("menu.html", menu = menu, name = session["user"], R = R, carrito = carrito)
else:
return render_template("menu.html", menu = menu, name = session["user"], R = R, carrito = carrito)
else:
return redirect("/LogIn")
这是我的HTML代码:
<form method="POST">
{% for x in menu %}
<div class="AlmacenB">
<a class="containerB">
<span class="b-background"><img class="BImg" src="/static/img/Menu/{{x[0]}}"></span>
<div class="store-infoB">
<div class="nombreEspacio">
<span class="bName">{{x[1]}}</span>
</div>
<span class = "Price">${{x[2]}}/Platillo</span>
<div class="cantidad">
<button class="MoreLess" onclick="deincrement('{{x[4]}}', {{x[3]}}); return false;">-</button>
<input class="cantidadAdd" id="{{x[4]}}" name="{{x[4]}}" value="0" readonly></input>
<button class="MoreLess" onclick="increment('{{x[4]}}', {{x[3]}}); return false;">+</button>
</div>
<div class="add">
<button class="addB" type="submit">Agregar</button>
</div>
</div>
</a>
</div>
<script>
count['{{x[4]}}'] = 0;
</script>
{% endfor %}
</form>
脚本部分仅适用于JS的按钮+和-,所以没关系
答案 0 :(得分:0)
请考虑以下情况:
您有多个+
按钮,因此将唯一值存储到value attribute
的{{1}}中。
因此按钮的外观如下:
button element
<button class="btn_add" value="1">+</button>
<button class="btn_add" value="2">+</button>
<button class="btn_add" value="3">+</button>
因此,无论您单击哪个按钮,都将获得被单击的按钮值。