2011-02-02 95 views
0

只需製作一個非常簡單的訂單。輸入數量並更新總訂單

想要一個輸入框(只接受數字)

用戶輸入的量,可以說50

我們有一個乘數值,其可以說10

想要禁用表單字段,表示50結果×10

所以表單字段會顯示500

所以現在我們有可變的OrderTotal我們可以通過我們的代碼,如:

$txtAmount = "orderTotal";

任何想法?

+0

你是什麼意思?傳遞php值給javascript? – 2011-02-02 06:38:12

+0

我想你想要一個簡單的購物車在網上很多教程,如http://v3.thewatchmakerproject.com/journal/276/building-a-simple-php-shopping-cart – 2011-02-02 06:41:48

+0

沒有不是PHP,實際上它的客戶端因此,所以js。只需要一個用戶可以輸入數字的輸入字段,以及將該數字乘以預設值的總字段。 – 422 2011-02-02 06:44:01

回答

1

代碼必須有點類似。但未經測試。如果您發現錯誤,那麼請讓我知道

編輯::測試代碼

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.js"></script> 
<script> 
    $(function(){ 


    $("#value").blur(
     function(){   
      var value = $(this).val(); 
      if(isInt(value)){ 
       $("#total").val($(this).val()*$("#quantity").val());     
      } 
      else{ 
       alert('pelase enter int'); 
      }  

     } 
    ); 
    $('#quantity').change(
     function() 
     { 
      var value =$("#value").val(); 
      if(isInt(value)) 
      { 
       if($("#quantity").val() > 0){ 
        $("#total").val($(this).val()*$("#value").val()); 
       }      
      } 
      else{ 
       alert('pelase enter int'); 
      } 
     } 
    ) 

    }); 

    function isInt(x) { 
    var y=parseInt(x); 
    if (isNaN(y)) return false; 
    return x==y && x.toString()==y.toString(); 
    } 
</script> 

<form> 
    <input id="value" name="value" /> 
    <select id="quantity"> 
    <option>10</option> 
    <option>20</option> 
    <option>30</option> 
    <option>40</option> 
    <option>50</option> 
    </select>  
</form> 
<input id="total" type="text" disabled="disabled"/> 

更新::這個應該工作,但不知道..如果再沒有工作讓我知道