2017-04-10 36 views
0

shopping cart image創建購物車的網站,subprice問題

我想創建subprice功能,顯示在colomn,但我不知道如何從第一個ID下面的數量打印subprice,請幫我:d 這個代碼是顯示了subprice

<div class="col-sm-2 text-center" id="test"> 
    <script> 
    $("input[name=quantity0]").on("keydown",function(){ 
    id= "<?php echo $id ;?>"; 
    var price; 
    quantity=$("input[name=quantity0]").val(); 
    for(i=0;i<carts.length;i++){ 
    if(id==1){ 
     price=30; 
     total=price*quantity; 
     $("#test").text("$"+total); 
    } 
    if(id==2){ 
     price=30; 
     total=price*quantity; 
     $("#test").text("$"+total); 
    } 
    if(id==3){ 
     price=30; 
     total=price*quantity; 
     $("#test").text("$"+total); 
    } 
    if(id==4){ 
     price=30; 
     total=price*quantity; 
     $("#test").text("$"+total); 
    } 
    if(id==5){ 
     price=30; 
     total=price*quantity; 
     $("#test").text("$"+total); 
    } 
    if(id==6){ 
     price=30; 
     total=price*quantity; 
     $("#test").text("$"+total); 
    } 
    if(id==7){ 
     price=30; 
     total=price*quantity; 
     $("#test").text("$"+total); 
    } 
    if(id==8){ 
     price=30; 
     total=price*quantity; 
     $("#test").text("$"+total); 
    } 
    if(id==9){ 
     price=30; 
     total=price*quantity; 
     $("#test").text("$"+total); 
    } 
    if(id==10){ 
     price=30; 
     total=price*quantity; 
     $("#test").text("$"+total); 
    } 
    } 
    }); 
    </script> 
</div> 

我在你們需要試着去了解問題的情況下上傳它在我的網站http://scellion.hol.es/:d

+0

這裏是你的代碼? – mlegg

+0

現在上傳我的代碼 – Sam

+0

ummm即時通訊新的,你們可以告訴我如何創建代碼框? :D – Sam

回答

0

在我看來,您應該先重新設計您的代碼,但目前我們很難正確回答您的問題。我不知道你的經驗水平,但我會給你一些小費。

開關罩

取而代之的是:

if(id==1){ 
    price=30; 
    total=price*quantity; 
    $("#test").text("$"+total); 
} 
if(id==2){ 
    price=30; 
    total=price*quantity; 
    $("#test").text("$"+total); 
} 
if(id==3){ 
    price=30; 
    total=price*quantity; 
    $("#test").text("$"+total); 
} 
if(id==4){ 
    price=30; 
    total=price*quantity; 
    $("#test").text("$"+total); 
} 

你應該使用這樣的:

switch(id):{ 
    case 1:{ 
     price=30; 
     total=price*quantity; 
     $("#test").text("$"+total); 
     break; 
    } 

    case 2:{ 
     price=30; 
     total=price*quantity; 
     $("#test").text("$"+total); 
     break; 
    } 

    case 3:{ 
     price=30; 
     total=price*quantity; 
     $("#test").text("$"+total); 
     break; 
    } 


    case 4:{ 
     price=30; 
     total=price*quantity; 
     $("#test").text("$"+total); 
     break; 
    } 

    default:{ 

    } 
} 

CREATE FUNCTION

直接複製的nd粘貼相同的代碼,你應該創建一個函數。功能對於軟件維護也很有用。例如:

function setText(){ 

     price=30; 
     total=price*quantity; 
     $("#test").text("$"+total); 
} 

switch(id):{ 
    case 1:{ 
     setText(); 
     break; 
    } 

    case 2:{ 
     setText(); 
     break; 
    } 

    case 3:{ 
     setText(); 
     break; 
    } 


    case 4:{ 
     setText(); 
     break; 
    } 

    default:{ 

    } 
} 

無論如何,如果我理解這個問題,這是我在您的網站在瀏覽器控制檯測試代碼,它似乎工作:

//THIS IS FOR INTERCEPT VALUE CHANGE (UP AND DOWN) 
$("input[name=quantity0]").bind('keyup mouseup', function() { 

    //JUST FOR TESTING, BUT YOU NEED TO TAKE THE REAL PRICE OF THE ITEM 
    var priceSingleUnit = 30; 

    //THIS.VALUE ---> TAKES THE VALUE OF THE INPUT FIELD 
    if(!(this.value <= 0)){ 
    $("#test").text("$" + (this.value * priceSingleUnit)); 
    } 

}); 

相關鏈接:

https://www.w3schools.com/js/js_switch.asp

https://www.w3schools.com/js/js_functions.asp

+0

謝謝你的提示,即時消息仍然是新的網絡編程,以及這是一個項目從講師創建購物車沒有數據庫 – Sam