我在$count
中有变量<div class="custom">
,如果另一个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;
}
答案 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"
答案 1 :(得分: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">
答案 2 :(得分:0)
尝试为:
$('.test1').on('change', function(e)) { $('.custom').html($(this).html()); }
答案 3 :(得分: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());
})
此外,当用户↹Tab s超出输入字段时的change
事件。如果您希望在更改<div>
的类时运行此函数,则更好的解决方案是在更改类时直接调用此函数,或者使用MutationObserver
。