添加到cart"与jQuery
"Add to cart" with jQuery
目标
复制一个文本并将其移动到另一个元素。
我的应用程序上有一个产品列表,当我点击"添加按钮"时,我想在摘要中添加它们。直到这里,轻松,对吧?!
汇总上的产品将显示他的名字和数量。所以我问:如何从DOM中提取这个名称并将其"粘贴"到新的位置?
但是…等等!当"add"按钮位于product元素之外时,问题就大了。我的意思是,"添加按钮"位于代码底部的工具提示中,与产品的元素没有关系。
我想做什么
工具提示的结构如下:
<div class="tooltip">
<form action="">
<input type="text" name="product_quantity" />
<button type="submit" />
</form>
</div>
我想这样做:
<div class="tooltip">
<form action="">
<input type="hidden" name="product_id" value="1" />
<input type="text" name="product_quantity" />
<button type="submit" />
</form>
</div>
然后,通过jQuery,获得具有该值的元素(在我们的例子中,1)。
我的官方代码
你可以在FiddleJS或上面看到它:
<ul>
<li>
<div class="product-header">
<h1>Cap</h1>
</div>
<div class="product-body">
<p>A beautiful cap</p>
</div>
<div class="product-controls">
<a href="#">Click here to open the tooltip to select a quantity then add to products summary</a>
</div>
</li>
<li>
<div class="product-header">
<h1>Gears of War — The game</h1>
</div>
<div class="product-body">
<p>TPS by Microsoft Studios</p>
<div class="product-controls">
<a href="#">Click here to open the tooltip to select a quantity then add to products summary</a>
</div>
</div>
</li>
</ul>
<div class="tooltip" style="display: none;">
<form action="">
<input type="text" name="product_quantity" />
<button type="submit" />
</form>
</div>
(更新)也许……AJAX ? !
我开始考虑恢复产品标识符的可能性(通过工具提示的输入隐藏),并对数据库进行查询-但这是可行的吗?
提前感谢!
//使用jquery获取产品id值
$('.tooltip button').click(function() {
alert($('#product_id').val());
});
如果您想使用AJAX,可以试试这个http://www.webresourcesdepot.com/creating-a-slick-ajaxed-add-to-basket-with-jquery-and-php/
几个月前我基于jQuery写了一个类似的东西:https://github.com/yckart/jquery.animateto.js
这是一个简单的演示,没有任何ajax请求或其他
http://yckart.github.com/jquery.animateto.js/相关文章:
- jquery validate 1.8.1”;jQuery"是未定义的错误IE8
- jQuery"或“;操作员没有处理mouseleave
- jQuery"焦点”;在一个实例中有效,但在其他实例中无效
- 如何分解jQuery"toggle”;函数转换为separate“;显示“;以及“;隐藏“;功能
- jQuery"悬停时放大”;无法相对于图片中心放大
- jQuery"return{foo:bar,foo2:bar2}"-它是什么
- jQuery"该参考文献“;加载事件后未设置
- jQuery"加载更多的帖子”;隐藏按钮如果最后一个帖子
- jQuery"不包含“;选择器
- jQuery"OR”;具有共享修饰符的选择器
- jQuery"谷歌加载器”;个人网站libs的模拟
- jQuery"滑动下降”;动画不起作用
- 如何使工作jquery"“滑动”;在iPad上滚动
- jQuery:"类“;使用对象数组
- jQuery:"点击“;绕过了.on()函数中的param
- 替换已弃用的jQuery"活的“;至“;关于“;使用RegEx格式化
- 使用jquery".在“;和“;.在“之前”;
- 为什么jquery"改变“;事件在我的示例中不起作用
- jQuery"isHTML5Error”-类似功能
- qTip2 | jQuery"关于“;方法