JQuery未注册按钮点击
JQuery not registering Button click
我有一个显示产品列表的页面。用户可以选择要订购的产品数量,然后点击订购按钮。这将在Cookie中放置对数量和产品ID的引用。然而,目前button
甚至没有注册。当我点击它时,什么也没发生。
如果我将按钮置于foreach
循环之外,则它可以工作。有人知道是怎么回事吗?
下面是产品列表的代码
<?php if($products): ?>
<ul>
<?php foreach($products as $product): ?>
<h3 id="stock_code"><?= get_field('stock_code', $product->ID); ?></h3>
<p>Description: <?= get_field('description', $product->ID); ?></p>
<p>Quantity Per Pallet: <?= get_field('quantity_per_pallet', $product->ID); ?></p>
<!-- Quantity Dropdown -->
Amount <select id="order_amount<?= $product->ID; ?>" name="amt">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="submit" value="Add to Order" id="orderBtn"/>
<?php endforeach; ?>
</ul>
<? endif ?>
这是点击button
时要执行的代码
$("#orderBtn").click(function(event){
//Show the order Box
$(".order-alert").show();
event.preventDefault();
//Create the Array
var productArray = [];
//If no Cookie exists, create one and add the Array
if ($.cookie('order_cookie') === undefined) {
console.log("Create a new cookie");
$.cookie('order_cookie', JSON.stringify(productArray), { expires: 1, path: '/' });
//If the Cookie already exists do this
} else {
console.log("Read the cookie");
productArray = JSON.parse($.cookie('order_cookie'));
console.log($.cookie('order_cookie'));
//Append items onto the Array
}
//Display the number of items in the Array in the Order Box
$('#order_counter').html(productArray.length);
});
在这件事上任何帮助我都很感激
不要在按钮上使用ID。如果你想给多个对象分配event,你必须使用class。
当你使用id选择器jQuery分配事件只给第一个对象,因为id应该是唯一的。
改变:
$("#orderBtn").click(function(event){
..
}
:
$(".orderBtn").click(function(event){
..
}
和HTML声明:
<input type="submit" value="Add to Order" id="orderBtn"/>
:
<input type="submit" value="Add to Order" class="orderBtn"/>
相关文章:
- 如何在单击“提交”按钮时为“新建”窗口编写JavaScript,用“确定”和“取消”显示注册信息
- 单击按钮时切换html登录/注册表单
- 设置重复按钮以注册点击事件jquery
- 我需要一个注册按钮,它可以将数据发送到mySQL服务器,也可以在单击时重定向
- 我需要在同一页面上同时使用“使用谷歌登录”和“使用谷歌注册”按钮
- 通过单击按钮或表单提交禁用注册按钮,这两种方式都会阻止表单提交
- JQuery 按钮单击未注册
- 如何在警报框关闭后将按钮的文本更改为“禁止注册!
- 单击注册具有相同类名的多个按钮
- 动态更新 url/html 标题时,如何获得类似 facebook 的按钮来注册更改?(不是 fb:like 中的 hr
- 如何返回选中正确单选按钮的注册页面
- 如何在弹出窗口中单击注册或登录按钮后保持弹出
- 两个登录/注册按钮是相同的.如何为同一个按钮提供 2 个功能
- 如果未选中复选框,如何禁用注册按钮
- 点击注册按钮生成一个随机数
- 单击整个元素以注册单选按钮值
- 断开类似facebook的按钮文本行“;一个喜欢.注册看看你的朋友喜欢什么
- Javascript/PHP事件更新注册按钮
- 如何在所有字段为true后激活注册按钮
- Jquery没有将我的一个单选按钮注册为选中