jquery代码未执行模板问题
我从未使用过jquery模板,但我的表单代码(“添加到购物车表单”)。在('submit',函数(事件)(代码如下)未执行,我相信这与模板有关。当我在控制台中运行代码时,它工作得很好jquery代码未执行模板问题,jquery,Jquery,我从未使用过jquery模板,但我的表单代码(“添加到购物车表单”)。在('submit',函数(事件)(代码如下)未执行,我相信这与模板有关。当我在控制台中运行代码时,它工作得很好 <script type="underscore/template" id="modal_template"> <form id="add-to-cart-form" action="/app/site/backend/additemtocart.nl" method="get">
<script type="underscore/template" id="modal_template">
<form id="add-to-cart-form" action="/app/site/backend/additemtocart.nl" method="get">
<div class="add-to-cart">
<div class="row">
<div class="dropdown inline pull-right">
<a data-toggle="dropdown" class="btn btn-primary btn-xs" href="#"><span class="fa fa-chevron-down"></span></a>
<ul id="qty-selector" class="dropdown-menu dropdown-menu-right" role="menu">
<li>
<a href="#" data-price="$<@= meal.price_six_pack @>" data-qty="6">
<span class="item-price pull-left">$<@= meal.price_six_pack @></span>
<span class="item-qty pull-right">Qty 6 or more</span>
<span class="clearfix"></span>
<span class="text-muted pull-left"><small>$<@=meal.price_discount@> per pack discount</small></span>
<span class="text-warning pull-right"><small>Save <@=meal.price_discount_percent@>%</small></span>
<span class="clearfix"></span>
</a>
</li>
</ul>
</div>
<div class="pull-right">
<span class="item-qty">Qty:</span>
<input type="text" class="item-qty form-control" id="current-qty" name="qty" value="1" onchange="updateQty()">
<input type="hidden" name="buyid" value='<%=getCurrentAttribute("item","internalid")%>'>
<input type="hidden" name="showcart" value="T">
</div>
<div class="clearfix"></div>
</div>
</div>
<div class="col-md-5">
<button type="submit" id="add-to-cart-btn" class="btn btn-lg btn-block btn-info">Add to Cart</button>
</div>
</div>
</form>
</script>
-
数量:
添加到购物车
下面的代码未执行
<script>
$("#add-to-cart-form").on('submit', function(event) {
var form = $(this);
event.preventDefault();
alert("testinggg")
$.ajax('/app/site/backend/additemtocart.nl', {
type: 'POST',
data: form.serialize(),
sucess: function(result) { }
});
});
</script>
$(“#添加到购物车表单”)。在('submit',函数(事件){
变量形式=$(此);
event.preventDefault();
警报(“测试GG”)
$.ajax(“/app/site/backend/additemtocart.nl”{
键入:“POST”,
数据:form.serialize(),
成功:函数(结果){}
});
});
在第一行
$("#add-to-cart-form")
我的意思是为它添加$。在某个时候,您必须使用模板将表单元素添加到页面中。如果绑定提交事件处理程序的代码在此之前执行,则调用
$(“#添加到购物车表单”)
创建一个空的jQuery对象。您需要在表单元素添加到页面后执行该代码,或者像这样使用事件委派
$(document).on('submit', '#add-to-cart-form', function(event) {
这会将事件处理程序绑定到文档(始终存在),但只有当事件源自与给定选择器匹配的元素(“添加到购物车表单”)时,它才会调用给定函数.很抱歉,我刚刚将其粘贴到错误的代码中。错误依然存在。控制台执行得很好,代码也可以工作……但直接将代码放入文件中却不行。我很确定这与尝试将其放入jQuery ready$(函数(){//put the code here})中有关太棒了,你救了我的星期五晚上!非常感谢!