将值存储在Java或javascript中,以便可以将其传递到另一个JSP [英] Store the value in java or in javascript so that it can be passed to another JSP

查看:50
本文介绍了将值存储在Java或javascript中,以便可以将其传递到另一个JSP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在此代码的第11行中获得总价的值 但是如何将其存储在java或javascript变量中,以便可以将该值以下面的形式传递给billing.jsp.

I am getting value of total price in the 11th line in this code but how can I store it in a java or javascript variable so that I can pass this value to billing.jsp in the below form.

-------------------------------------- HTML -------- ------------------------

--------------------------------------HTML--------------------------------

  <div class="card-block">
<h4 class="card-title">Orange</h4>
<p class="card-text">Price: $5</p>
<a href="#" data-name="Orange" data-price="5">Add to cart</a> // Price of 
Orange gets added to Cart
</div>

<div class="card-block">
<h4 class="card-title">Banana</h4>
<p class="card-text">Price: $5</p>
<a href="#" data-name="Banana" data-price="5">Add to cart</a> // Price of 
Banana gets added to Cart 
</div>          

 Total price: $<span class="total-cart"></span>  // Here I am getting 
 total price of orange and banana lets say $10, how can I store this 
dynamically generated value to a javascript variable or in any other 
suitable java variable so that I can pass the value as input type hidden 
 in the below form??


   <div class="footer">
      <form name="billing" action="billing.jsp">           
    <input type="hidden" name="price" value=""/> // How can I pass this 
 total price $10 here to billing.jsp here 
    <input type="submit"/>
   </form>        
  </div>

---------------------- JAVASCRIPT ----------------------

----------------------JAVASCRIPT----------------------

var shoppingCart = (function() {
 // =============================
 // Private methods and propeties
 // =============================
 cart = [];

// Constructor
 function Item(name, price, count) {
 this.name = name;
 this.price = price;
 this.count = count;
 }

  // Save cart
  function saveCart() {
  sessionStorage.setItem('shoppingCart', JSON.stringify(cart));
  }

  // Load cart
  function loadCart() {
   cart = JSON.parse(sessionStorage.getItem('shoppingCart'));
  }
   if (sessionStorage.getItem("shoppingCart") != null) {
    loadCart();
   }



  // Public methods and properties

  var obj = {};

  // Add to cart

  obj.addItemToCart = function(name, price, count) {
  for(var item in cart) {
  if(cart[item].name === name) {
    cart[item].count ++;
    saveCart();
    return;
  }
  }
  var item = new Item(name, price, count);
  cart.push(item);
  saveCart();
  }
  // Set count from item
  obj.setCountForItem = function(name, count) {
  for(var i in cart) {
  if (cart[i].name === name) {
    cart[i].count = count;
    break;
  }
 }
 };
 // Remove item from cart
  obj.removeItemFromCart = function(name) {
  for(var item in cart) {
    if(cart[item].name === name) {
      cart[item].count --;
      if(cart[item].count === 0) {
        cart.splice(item, 1);
      }
      break;
    }
  }
  saveCart();
  }

  // Remove all items from cart
  obj.removeItemFromCartAll = function(name) {
  for(var item in cart) {
  if(cart[item].name === name) {
    cart.splice(item, 1);
    break;
  }
 }
 saveCart();
 }

 // Clear cart
 obj.clearCart = function() {
 cart = [];
 saveCart();
 }

 // Count cart 
 obj.totalCount = function() {
  var totalCount = 0;
  for(var item in cart) {
  totalCount += cart[item].count;
  }
 return totalCount;
 }

  // Total cart
  obj.totalCart = function() {
  var totalCart = 0;
  for(var item in cart) {
  totalCart += cart[item].price * cart[item].count;
  }
  return Number(totalCart.toFixed(2));
  }

 // List cart
 obj.listCart = function() {
 var cartCopy = [];
 for(i in cart) {
  item = cart[i];
  itemCopy = {};
  for(p in item) {
    itemCopy[p] = item[p];

  }
  itemCopy.total = Number(item.price * item.count).toFixed(2);
  cartCopy.push(itemCopy)
 }
 return cartCopy;
 }

// cart : Array
// Item : Object/Class
// addItemToCart : Function
// removeItemFromCart : Function
// removeItemFromCartAll : Function
// clearCart : Function
// countCart : Function
// totalCart : Function
// listCart : Function
// saveCart : Function
// loadCart : Function
return obj;
})();


// *****************************************
// Triggers / Events
// ***************************************** 
// Add item
 $('.add-to-cart').click(function(event) {
  event.preventDefault();
  var name = $(this).data('name');
  var price = Number($(this).data('price'));
  shoppingCart.addItemToCart(name, price, 1);
  displayCart();
 });

 // Clear items
 $('.clear-cart').click(function() {
 shoppingCart.clearCart();
  displayCart();
  });


  function displayCart() {
  var cartArray = shoppingCart.listCart();
  var output = "";
  for(var i in cartArray) {
  output += "<tr>"
  + "<td>" + cartArray[i].name + "</td>" 
  + "<td>(" + cartArray[i].price + ")</td>"
  + "<td><div class='input-group'><button class='minus-item input-group- 
  addon btn btn-primary' data-name=" + cartArray[i].name + ">-</button>"
  + "<input type='number' class='item-count form-control' data-name='" + 
   cartArray[i].name + "' value='" + cartArray[i].count + "'>"
  + "<button class='plus-item btn btn-primary input-group-addon' data- 
  name=" 
  + cartArray[i].name + ">+</button></div></td>"
  + "<td><button class='delete-item btn btn-danger' data-name=" + 
  cartArray[i].name + ">X</button></td>"
  + " = " 
  + "<td>" + cartArray[i].total + "</td>" 
  +  "</tr>";
  }
  $('.show-cart').html(output);
  $('.total-cart').html(shoppingCart.totalCart());
  $('.total-count').html(shoppingCart.totalCount());
  }

 // Delete item button

  $('.show-cart').on("click", ".delete-item", function(event) {
  var name = $(this).data('name')
  shoppingCart.removeItemFromCartAll(name);  
   displayCart();
  })


 // -1
 $('.show-cart').on("click", ".minus-item", function(event) {
  var name = $(this).data('name')
  shoppingCart.removeItemFromCart(name);
  displayCart();
 })
  // +1
  $('.show-cart').on("click", ".plus-item", function(event) {
   var name = $(this).data('name')
  shoppingCart.addItemToCart(name);
  displayCart();
  })

  // Item count input
  $('.show-cart').on("change", ".item-count", function(event) {
   var name = $(this).data('name');
  var count = Number($(this).val());
  shoppingCart.setCountForItem(name, count);
  displayCart();
 });

 displayCart();

推荐答案

在下面的函数中,您正在调用totalCart():

Here in below function you are calling totalCart() :

function displayCart() {
  ..
  $('.show-cart').html(output);
 <!--here you got that total price-->
  $('.total-cart').html(shoppingCart.totalCart());
  $('.total-count').html(shoppingCart.totalCount());
  }

然后从function下面得到total price,只需在其中添加document.getElementById('idofinput').value = something,这样每当调用此函数时,总值就会在input中更新,也不要忘记给到您的input .ie:

And from below function you are getting total price,just add there document.getElementById('idofinput').value = something ,so whenever this function will be called the total value will get updated in input,also don't forget to give id to your input.i.e :

 // Total cart
      obj.totalCart = function() {
      var totalCart = 0;
      for(var item in cart) {
      totalCart += cart[item].price * cart[item].count;
      }
  //setting value for input field.
    document.getElementById('idofinput').value= Number(totalCart.toFixed(2));
      return Number(totalCart.toFixed(2));
      }

这篇关于将值存储在Java或javascript中,以便可以将其传递到另一个JSP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆