2016-08-06 19 views
0

我有一個<div class="custom">內的變量$count,如果另一個div類發生變化,我需要重新調整它。我寫了這樣的代碼如何刷新變化事件中的div內的變量?

$('.test1').on('change', function(e)) { 
    $('.custom').text($(this).text()); 
} 

但它不刷新這個變量。

這裏是$ prod_count變量DIV塊

function commerce_popup_cart_block_view($delta=''){ 
    $block = array(); 
    switch($delta) {  
    case 'commerce_popup_cart': 
     global $user; 
     // Default to an empty cart block message. 
     $content = ''; 
     // First check to ensure there are products in the shopping cart. 
     if ($order = commerce_cart_order_load($user->uid)) { 
     $wrapper = entity_metadata_wrapper('commerce_order', $order); 

      // Build the variables array to send to the cart block template. 
      $variables = array(
      'order' => $order, 
      'contents_view' => commerce_embed_view('commerce_cart_block',  'defaults', array($order->order_id), $_GET['q']), 
     ); 
      $count = commerce_line_items_quantity($wrapper->commerce_line_items, commerce_product_line_item_types());   
      $quantity = 0;   
      foreach ($wrapper->commerce_line_items as $line_item) { 
      if (!$line_item instanceof EntityMetadataWrapper) { 
       $line_item = entity_metadata_wrapper('commerce_line_item', $line_item); 
      } 
      $types = array('product'); 

      if (empty($types) || in_array($line_item->type->value(), $types)) { 
       $quantity = $quantity + $line_item->quantity->value(); 

      } 
      } 

      $prod_count = t($quantity); 

      if ($prod_count > 0){         
      $icon = '<div class="cart-icon"></div><div class="cart_popup_count">'. $prod_count . '</div>'; 
      $content = '<div id="cart-popup" style="display:none;">' . theme('commerce_cart_block', $variables) . '<div class="popup-arrow"></div></div>'; 
      $content = '<div class="wrapper">' . $icon . $content . '</div>'; 
      } elseif (variable_get('commerce_popup_cart_show_empty_cart', 0) == 1){ 
      $content = commerce_popup_cart_block_view_get_empty_cart($variables); 
      } 
     }elseif (variable_get('commerce_popup_cart_show_empty_cart', 0) == 1){   
      $content = commerce_popup_cart_block_view_get_empty_cart($variables = array());   
     } 

     // If the superfish module is not installed then add hoverintent script 
     if (!module_exists('superfish')){ 
     drupal_add_js(drupal_get_path('module','commerce_popup_cart') . '/js/jquery.hoverIntent.minified.js'); 
     } 

     return array('subject' => t('Shopping cart'), 'content' => $content); 
     break; 
    } 
    return $block; 
} 

截圖:
screenshot

+0

你有一個錯字:'function(e)){'而不是'function(e){'。 –

+0

秒)在事件 – rick1

+0

上關閉。不需要在函數聲明的結束'}之後執行。 –

回答

0

你應該有聲明一個全局變量,那麼你要使用某種類型的命名空間。只要在外面聲明名稱空間,那麼你可以拋出任何你想要的東西。例如

var YOUR_VALUE = {}; 
$(document).ready(function() { 
    YOUR_VALUE.check = ""; 

    YOUR_VALUE.check = "<?php echo $php_VAR; ?>";"; 
}); 

console.log(YOUR_VALUE.check); // "TEST CODE" 
+0

我想要刷新的變量是php變量 – rick1

0

試試這個代碼

<script type="text/javascript">  
    function myfunction(val) 
    { 
     $('.custom').val(val); 
    } 
    </script> 


    <input type="text" class="test1" id="test1" onkeyup="myfunction(this.value);"> 
    <input type="text" class="custom" id="custom"> 
+0

這段代碼不工作,因爲它應該是jquery – rick1

+0

這段代碼對我來說工作正常.i認爲你有jquery衝突問題。你只需將我的代碼添加到另一個文件並運行 –

+0

爲什麼你在這裏使用輸入?我不需要在HTML文件中添加此代碼,它應該在script.js – rick1

0

嘗試,因爲:

$('.test1').on('change', function(e)) { $('.custom').html($(this).html()); } 
+0

謝謝,但它不起作用 – rick1

+0

您需要獲取div值的更改並將其分配給另一個,如上面修改。 –

+0

我需要刷新test1而不是自定義作爲需要值的變量是自定義內部所以如果更改自定義,test1必須更改以及 – rick1

0

它不工作,因爲你有一個語法錯誤:

$('.test1').on('change', function(e)) { 
    $('.custom').text($(this).text()); 
} 
// Output: SyntaxError: Unexpected token ')'. Expected an opening '{' at the start of a function body. 

正確語法是:

$('.test1').on('change', function(e) { 
    $('.custom').text($(this).text()); 
}) 

此外,change事件當用戶↹標籤出去了的輸入字段的。如果您想在<div>的類更改時運行此功能,更好的解決方案可能是在更改類時直接調用此函數,或者使用MutationObserver s。

+0

是我修改了語法,但它並沒有幫助我... – rick1

+0

@ rick1請澄清正是你想要在這裏做的。我無法理解它。 –

+0

如果購物車內的產品數量發生變化,我想更改卡片外的產品數量,如圖片 – rick1