Witam,

Na wstęp kod:

  1. <fieldset id='topicStyle_fieldset'>
  2. <h3 class='bar'>Ustawienia Forum Gold</h3>
  3. <ul style='border-bottom: 1px solid #D5DDE5'>
  4. <li class='field'>
  5. <label for='topicColor'>Kup <strong>kolorowy</strong> temat:</label>
  6. <input class='input_text color {hash:true}' type='text' id='topicColor' name='topicColor' value="{$formData['topicColor']}" onChange='sumPrice( this );' />
  7. <span class='desc'>Wpisz kod koloru HEX lub wybierz klikając na pole koloru.</span>
  8. </li>
  9. <li class='field'>
  10. <label for='topicShadow'>Użyć <span style='font-weight: bold; text-shadow: 1px 1px 2px #FF0000'>cieni</span> w temacie?</label>
  11. <input class='input_text color {hash:true}' type='text' id='topicShadow' name='topicShadow' value="{$formData['topicShadow']}" onChange='sumPrice( this );' />
  12. <span class='desc'>Użycie cieni i ich przesunięć jest niemożliwe! Można zmienić jedynie ich kolor.</span>
  13. </li>
  14. <li class='field checkbox'>
  15. <input class='input_check' type='checkbox' id='topicBold' name='topicBold' value="{$formData['topicBold']}" <if test="$formData['topicBold']">checked='checked'</if> onClick='sumPrice( this );' />
  16. <label for='topicBold'>Włączyć <b>pogrubienie</b>?</label>
  17. </li>
  18. <li class='field checkbox'>
  19. <input class='input_check' type='checkbox' id='topicItalic' name='topicItalic' value="{$formData['topicItalic']}" <if test="$formData['topicItalic']">checked='checked'</if> onClick='sumPrice( this );' />
  20. <label for='topicItalic'>Włączyć <i>kursywę</i>?</label>
  21. </li>
  22. </ul>
  23. <ul>
  24. <li class='field'>
  25. <label><strong>Całkowity koszt</strong>:</label>
  26. <span class='totalPrice_ok' id='totalPriceLabel'></span> Forum Gold
  27. </li>
  28. </ul>
  29. <script type='text/javascript'>
  30. /* Prices */
  31. var topicColor_price = parseFloat( 15.00 );
  32. var topicShadow_price = parseFloat( 30.00 );
  33. var topicBold_price = parseFloat( 10.00 );
  34. var topicItalic_price = parseFloat( 10.00 );
  35.  
  36. /* INIT */
  37. var totalPrice = 0;
  38. var yourForumGold = parseFloat( 50 );
  39.  
  40. /**
  41. * Count Amount to buy items
  42. *
  43. * @param object HTML Object
  44. * @return void
  45. */
  46. function sumPrice( elementID )
  47. {
  48. if( $( elementID ) == null ) { return getTotalAmount(); }
  49.  
  50. /* Finding actions... */
  51. var elementObject = $( elementID );
  52.  
  53. /* Get value of price! */
  54. var elementPrice = elementObject.id + '_price';
  55.  
  56. /* Sum... */
  57. totalPrice += eval( elementPrice );
  58.  
  59. /* More than You can buy? */
  60. if( totalPrice > yourForumGold )
  61. {
  62. $('totalPriceLabel').className = 'totalPrice_fail';
  63. }
  64. else
  65. {
  66. $('totalPriceLabel').className = 'totalPrice_ok';
  67. }
  68.  
  69. getTotalAmount();
  70. }
  71.  
  72. /**
  73. * Return Amount
  74. *
  75. * @param void
  76. * @return string
  77. */
  78. function getTotalAmount()
  79. {
  80. /* Save total amount :-) */
  81. $('totalPriceLabel').innerHTML = totalPrice;
  82. }
  83. sumPrice();
  84. </script>


W czym stoi rzecz? Mam listę i w niej pewne elementy, w tym 2 inputy i 2 checkBox. Każda ich zmiana jak widać powoduje dodanie kwoty. I tu pojawia się problem. Bo za każdą zmianą dodaje, a ja nie mam pomysłu jak to zapisać, aby zapisywał stan zmiennych i w zależności od ich wartości ( w tym szczególnie inputów ) odejmował kwoty. Pomożecie?

Pozdrawiam,
Largo

  1. <script type='text/javascript'>
  2. /* Prices */
  3. var availablePrices = new Hash( { 'topicColor_price' : 15.00, 'topicShadow_price' : 30.00, 'topicBold_price' : 10.00, 'topicItalic_price' : 5.00 } );
  4.  
  5. /* States */
  6. var availableStates = new Hash( { 'topicColor_state' : 0, 'topicShadow_state' : 0, 'topicBold_state' : 0, 'topicItalic_state' : 0 } );
  7.  
  8. /* INIT */
  9. var totalPrice = 0.00;
  10. var yourForumGold = parseFloat( {$this->memberData['member_fg']} );
  11. /**
  12. * Count Amount to buy items
  13. *
  14. * @param object HTML Object
  15. * @return void
  16. */
  17. function sumPrice( elementID )
  18. {
  19. if( $( elementID ) == null ) { return getTotalAmount(); }
  20.  
  21. /* Finding actions... */
  22. var elementObject = $( elementID );
  23.  
  24. /* Get value of price! */
  25. var elementPrice = elementObject.id + '_price';
  26.  
  27. /* Sum... */
  28. if( availableStates.get( elementObject.id + '_state' ) )
  29. {
  30. totalPrice -= availablePrices.get( elementPrice );
  31. }
  32. else
  33. {
  34. totalPrice += availablePrices.get( elementPrice );
  35. }
  36.  
  37. /* More than You can buy? */
  38. if( totalPrice > yourForumGold )
  39. {
  40. $('totalPriceLabel').className = 'totalPrice_fail';
  41. }
  42. else
  43. {
  44. $('totalPriceLabel').className = 'totalPrice_ok';
  45. }
  46.  
  47. getTotalAmount();
  48. }
  49.  
  50. /**
  51. * Save State
  52. *
  53. * @param string ID of HTML Element
  54. * @return void
  55. */
  56. function saveState( stateName, isCheckbox )
  57. {
  58. if( $( stateName ) )
  59. {
  60. /* Checkbox? */
  61. if( isCheckbox )
  62. {
  63. if( $( stateName ).checked )
  64. {
  65. availableStates.set( stateName + '_state', 1 );
  66. }
  67. else
  68. {
  69. availableStates.set( stateName + '_state', 0 );
  70. }
  71. }
  72. else
  73. {
  74. /* Normal box? */
  75. if( $F('topicColor').blank() )
  76. {
  77. availableStates.set( stateName + '_state', 1 );
  78. totalPrice -= availablePrices.get( stateName + '_price' );
  79. }
  80. else
  81. {
  82. availableStates.set( stateName + '_state', 0 );
  83. }
  84. }
  85. }
  86. }
  87.  
  88. /**
  89. * Return Amount
  90. *
  91. * @param void
  92. * @return string
  93. */
  94. function getTotalAmount()
  95. {
  96. /* Save total amount :-) */
  97. $('totalPriceLabel').innerHTML = totalPrice.toFixed(2);
  98. }
  99. sumPrice();
  100. </script>


Prawie sobie sam poradziłem, ale dalej mam jeden problem. Dla checkBox te operacje są poprawne, ale dla input nie. Nie mogę określić czy dany input ma treść. Jeżeli ma, dostaję raz cenę, a nie X razy na kliknięcie, ale to sobie zrobię. Jak się dostać do wartości input? Używam Prototype, a Color Picker jest pobrany stąd.