2014-09-01 131 views
0

什麼是計算我的購物車價格的最佳方法?從數據庫中獲取價格或將其放入代碼中會更好嗎?到目前爲止,我把它放在我的代碼,但它似乎沒有奏效:jQuery添加多個項目

<script> 
var b = 0, v = 0 
$("input[name='compcase'], input[name='caselight']").change(function() { 
    b=$("input[name='compcase']:checked"); 
    v=$("input[name='caselight']:checked"); 
    b=b.length==0?0:parseInt(b.attr('data-price')); 
    v=v.length==0?0:parseInt(v.attr('data-price')); 
    $('.test').html(b+v); 
}); 
</script> 
<body> 
<p class="test"></p><br><br> 

<Form name ="pc" Method ="Post" ACTION ="radiobutton.php"> 
<img src="http://www.pureoverclock.com/wp-content/uploads/images/review/cases/nzxt_phantom410/nzxt_phantom410_4.jpg" width="150" height="180"></img><br> 
<Input type = 'Radio' Name ='compcase' value= '1' data-price='25' />NZXT Phantom Enthusiast USB3.0 Full Tower Case - White <br /> 
<Input type = 'Radio' Name ='compcase' value= '2' data-price='25' />Corsair Obsidian 750D Large Tower Case Black <br /> 
<Input type = 'Radio' Name ='compcase' value= 'Cooler Master CM Storm Trooper' data-price='25' />Cooler Master CM Storm Trooper Black Full Tower Gaming Case <br /><br /> 

<Input type = 'Radio' Name ='caselight' value= 'Red' data-price='60' />Red<br /> 
<Input type = 'Radio' Name ='caselight' value= 'Green' data-price='60' />Green <br /><br /> 

編輯:

好了,所以。基本上,根據我單擊的單選按鈕,到目前爲止,我只有一個爲價格設置的計算表,小計會改變。這裏是我的表:http://gyazo.com/9fe4daf2d76b2174fb0f6f6fdb04aaf4這裏是我的單選按鈕:http://gyazo.com/fbcfd7814bb3a03ad768ae33bb0d37eb

+0

工作正常** [這裏](HTTP:/ /plnkr.co/edit/XvGoJgxX3utozFmfuM2i?p=preview)**你忘了包裝在一個準備好的處理程序? – charlietfl 2014-09-01 21:45:52

+0

非常感謝!這有助於 – 2014-09-01 22:03:35

+0

現在我有一個問題:以某種方式從數據庫中的表格中獲取價格會更好,而不是這樣做,因爲價格可能每天都在波動,並且可能會有像這樣的許多頁面,所以它看起來像時間的腰通過代碼,當我可以在數據庫中更新它? – 2014-09-01 22:05:20

回答

0

只是供參考,

如果你不依靠這個價格的購物車過程結束它沒關係。否則,你會遇到嚴重的問題,產品被低價出售。

一個sugestion將更新值的要求,這樣你可以應用在購物車和許多其他的事情多itens折扣...

對不起,我不能用我的名譽評論..

@UPDATE

你可以得到所有選中的電臺:

var checkedItems = $('input[type="radio"]:checked'); 

並將其發送到一個PHP(或其他東西)處理並返回ÿ什麼OU需要:

$.post('url/to/a/processor', checkedItems.serialize(), function(data) { 
    //update the cart price from data 
}); 

在後端:

<?php 
// Connects to your Database 
mysql_connect("localhost", "root", "") or die(mysql_error()); 
mysql_select_db("test") or die(mysql_error()); 
$checkedCompcase = isset($_POST['compcase']) && !empty($_POST['compcase']) ? $_POST['compcase'] : null; 
$data = mysql_query("SELECT * FROM compcase WHERE id IN ($checkedCompcase)") or die(mysql_error()); 

// treat data and sum up your cart, apply discount, whatever you need 

// return data 
header('Content-Type: application/json', true); 
// define what you need in the frontend and return what is expected 
echo json_encode(array('cart' => array('value' => $sum, 'items' => array())); 

只是不要忘了把發送到數據庫中的所有數據查詢

+0

你能舉個例子嗎? – 2014-09-01 22:52:04

+0

這是很容易連接到數據庫或不? – 2014-09-01 23:00:57

+0

不好意思,我可以用這個做什麼,非常感謝 – 2014-09-01 23:05:44

0

你缺少你變量聲明後,分號:

var b = 0, v = 0; //<--you missed this 

隨着那固定的,它工作正常,按本琴:http://jsfiddle.net/3mjkw7w9/

+0

分號不會在換行時產生差異。不是最佳做法,但不是代碼斷路器 – charlietfl 2014-09-01 22:05:48