如何使用jquery限制可拖放div(购物车)接受的项目数量
我对jquery非常陌生。我想限制在可拖放购物车中拖动的项目数量 购物车应该只接受三个项目的下降后,它不应该接受任何项目 jsfiddle: 我试过所有其他的复制品,但对我不起作用。。 外面有人请帮帮我如何使用jquery限制可拖放div(购物车)接受的项目数量,jquery,jquery-ui,drag-and-drop,shopping-cart,jquery-easyui,Jquery,Jquery Ui,Drag And Drop,Shopping Cart,Jquery Easyui,我对jquery非常陌生。我想限制在可拖放购物车中拖动的项目数量 购物车应该只接受三个项目的下降后,它不应该接受任何项目 jsfiddle: 我试过所有其他的复制品,但对我不起作用。。 外面有人请帮帮我 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="keywords" content="jquery,ui,easy,easyui,web">
<meta name="description" content="easyui help you build your web page easily!">
<title>jQuery EasyUI Demo</title>
<link rel="stylesheet" type="text/css" href="http://www.jeasyui.com/easyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="http://www.jeasyui.com/easyui/themes/icon.css">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.1.min.js"></script>
<script type="text/javascript" src="http://www.jeasyui.com/easyui/jquery.easyui.min.js"></script>
<style type="text/css">
.products{
list-style:none;
margin-right:300px;
padding:0px;
height:100%;
}
.products li{
display:inline;
float:left;
margin:10px;
}
.item{
display:block;
text-decoration:none;
}
.item img{
border:1px solid #333;
}
.item p{
margin:0;
font-weight:bold;
text-align:center;
color:#c3c3c3;
}
.cart{
position:fixed;
right:0;
top:0;
width:300px;
height:100%;
background:#ccc;
padding:0px 10px;
}
h1{
text-align:center;
color:#555;
}
h2{
position:absolute;
font-size:16px;
left:10px;
bottom:20px;
color:#555;
}
.total{
margin:0;
text-align:right;
padding-right:20px;
}
</style>
<script>
var data = {"total":0,"rows":[]};
var totalCost = 0;
$(function(){
$('#cartcontent').datagrid({
singleSelect:true
});
$('.item').draggable({
revert:true,
proxy:'clone',
onStartDrag:function(){
$(this).draggable('options').cursor = 'not-allowed';
$(this).draggable('proxy').css('z-index',10);
},
onStopDrag:function(){
$(this).draggable('options').cursor='move';
}
});
$('.cart').droppable({
onDragEnter:function(e,source){
$(source).draggable('options').cursor='auto';
},
onDragLeave:function(e,source){
$(source).draggable('options').cursor='not-allowed';
},
onDrop:function(e,source){
var name = $(source).find('p:eq(0)').html();
var price = $(source).find('p:eq(1)').html();
addProduct(name, parseFloat(price.split('$')[1]));
}
});
});
function addProduct(name,price){
function add(){
for(var i=0; i<data.total; i++){
var row = data.rows[i];
if (row.name == name){
row.quantity += 1;
return;
}
}
data.total += 1;
data.rows.push({
name:name,
quantity:1,
price:price
});
}
add();
totalCost += price;
$('#cartcontent').datagrid('loadData', data);
$('div.cart .total').html('Total: $'+totalCost);
}
</script>
</head>
<body style="margin:0;padding:0;height:100%;background:#fafafa;">
<ul class="products">
<li>
<a href="#" class="item">
<img src="images/shirt1.gif"/>
<div>
<p>Balloon</p>
<p>Price:$25</p>
</div>
</a>
</li>
<li>
<a href="#" class="item">
<img src="images/shirt2.gif"/>
<div>
<p>Feeling</p>
<p>Price:$25</p>
</div>
</a>
</li>
<li>
<a href="#" class="item">
<img src="images/shirt3.gif"/>
<div>
<p>Elephant</p>
<p>Price:$25</p>
</div>
</a>
</li>
<li>
<a href="#" class="item">
<img src="images/shirt4.gif"/>
<div>
<p>Stamps</p>
<p>Price:$25</p>
</div>
</a>
</li>
<li>
<a href="#" class="item">
<img src="images/shirt5.gif"/>
<div>
<p>Monogram</p>
<p>Price:$25</p>
</div>
</a>
</li>
<li>
<a href="#" class="item">
<img src="images/shirt6.gif"/>
<div>
<p>Rolling</p>
<p>Price:$25</p>
</div>
</a>
</li>
</ul>
<div class="cart">
<h1>Shopping Cart</h1>
<div style="background:#fff">
<table id="cartcontent" fitColumns="true" style="width:300px;height:auto;">
<thead>
<tr>
<th field="name" width=140>Name</th>
<th field="quantity" width=60 align="right">Quantity</th>
<th field="price" width=60 align="right">Price</th>
</tr>
</thead>
</table>
</div>
<p class="total">Total: $0</p>
<h2>Drop here to add to cart</h2>
</div>
</body>
</html>
jqueryeasyui演示
.产品{
列表样式:无;
右边距:300px;
填充:0px;
身高:100%;
}
李先生{
显示:内联;
浮动:左;
利润率:10px;
}
.项目{
显示:块;
文字装饰:无;
}
.项目img{
边框:1px实心#333;
}
.项目p{
保证金:0;
字体大小:粗体;
文本对齐:居中;
颜色:#C3C3;
}
.购物车{
位置:固定;
右:0;
排名:0;
宽度:300px;
身高:100%;
背景:#ccc;
填充:0px 10px;
}
h1{
文本对齐:居中;
颜色:#555;
}
氢{
位置:绝对位置;
字体大小:16px;
左:10px;
底部:20px;
颜色:#555;
}
.总数{
保证金:0;
文本对齐:右对齐;
右边填充:20px;
}
变量数据={“总计”:0,“行”:[]};
var总成本=0;
$(函数(){
$('#cartcontent').datagrid({
singleSelect:true
});
$('.item')。可拖动({
回复:对,
代理:'clone',
onStartDrag:function(){
$(this).draggable('options')。游标='不允许';
$(this).draggable('proxy').css('z-index',10);
},
onStopDrag:function(){
$(this).draggable('options').cursor='move';
}
});
$('.cart')。可拖放({
onDragEnter:函数(e,源){
$(源).draggable('options').cursor='auto';
},
onDragLeave:函数(e,源){
$(source).draggable('options')。cursor='not-allowed';
},
onDrop:函数(e,源){
var name=$(source.find('p:eq(0)').html();
var price=$(source.find('p:eq(1)').html();
addProduct(名称,parseFloat(price.split(“$”)[1]);
}
});
});
功能添加产品(名称、价格){
函数add(){
对于(var i=0;i给你..修改addProduct函数以检查数据中的3项。总计
。如果没有,则发出警报
试试这个
function addProduct(name,price){
if(data.total < 3){
function add(){
for(var i=0; i<data.total; i++){
var row = data.rows[i];
if (row.name == name){
row.quantity += 1;
return;
}
}
data.total += 1;
data.rows.push({
name:name,
quantity:1,
price:price
});
}
add();
totalCost += price;
$('#cartcontent').datagrid('loadData', data);
$('div.cart .total').html('Total: $'+totalCost);
}else{
alert('cannot have more than 3 itrems');
}
}
function addProduct(name,price){
var totalQuantity=sumQuantity(data);
if(totalQuantity < 3){
function add(){
for(var i=0; i<data.total; i++){
var row = data.rows[i];
if (row.name == name){
row.quantity += 1;
return;
}
}
data.total += 1;
data.rows.push({
name:name,
quantity:1,
price:price
});
}
add();
totalCost += price;
$('#cartcontent').datagrid('loadData', data);
$('div.cart .total').html('Total: $'+totalCost);
}else{
alert('cannot have more than 3 itrems');
}
}
//new function to sum up all the quantity.
function sumQuantity(data){
var sum=0;
for(var i=0; i<data.total; i++){
sum += data.rows[i].quantity;
}
return sum;
}
功能添加产品(名称、价格){
如果(数据总量<3){
函数add(){
对于(var i=0;这正是我所要求的,但是如果我们删除同一项三次或其中一项两次,然后再删除另一项,那么这就不起作用了。你有解决方案吗??