简体   繁体   English

从java脚本中获取值

[英]Get value from java script

I have a javascript file and there is a function to calculate total and I want to use the calculated total value in javascript in my main HTML or php page what can I do?我有一个 javascript 文件,并且有一个计算总数的函数,我想在我的主 HTML 或 php 页面中使用 javascript 中计算出的总值,我该怎么办? I tried using sessions but it did not work.我尝试使用会话,但没有用。

This is my javascript code.这是我的 javascript 代码。 How can I get that total in my main page as a variable.我怎样才能在我的主页中将总数作为一个变量。 I tried using getElementBy function to but it did not work.我尝试使用getElementBy函数,但没有用。

 function updateCartTotal() {
    var cartItemContainer = document.getElementsByClassName('cart-items')[0]
    var cartRows = cartItemContainer.getElementsByClassName('cart-row')
    var total = 0
    for (var i = 0; i < cartRows.length; i++) {
        var cartRow = cartRows[i]
        var priceElement = cartRow.getElementsByClassName('cart-price')[0]
        var quantityElement = cartRow.getElementsByClassName('cart-quantity-input')[0]
        var price = parseFloat(priceElement.innerText.replace('$', ''))
        var quantity = quantityElement.value
        total = total + (price * quantity)
    }
    total = Math.round(total * 100) / 100
    console.log(total)
    document.getElementsByClassName('cart-total-price')[0].innerText = '$' + total
    document.cookie ='total='+total+';expires=Wed, 1 Jan 2070 13:47:11 UTC; path=/'
    //var x =document.cookie
    console.log(x)
    console.log(input)


}
  1. You can pass this value (from javascript function) to html( dom ) with dom-manipulation term, example:您可以使用dom-manipulation术语将此值(来自javascript函数)传递给 html( dom ),例如:
document.getElementById("demo").innerHTML = "Your Data";

Read More 阅读更多

  1. You can pass value from client to server with cookie (not session ) and get it on server ( php or whatever), example:您可以使用cookie (而不是session )将值从客户端传递到服务器并在服务器( php或其他)上获取它,例如:
document.cookie = "username=YOUR_DATA; expires=Thu, 18 Dec 2013 12:00:00 UTC; path=/";

Read More阅读更多

  1. You can send data from client to the server with AJAX call, example:您可以使用 AJAX 调用将数据从客户端发送到服务器,例如:
xhttp.open("POST", "getinfo.php", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("data=yourData&other=another"); // pass data here

Read More阅读更多

if your'e just looking to get the total into the "cart-price" class name element the code below should work.如果您只是想将总数添加到"cart-price"类名称元素中,则下面的代码应该可以工作。 I just changed anywhere you put element.getElementsByClassName('class-name') to document.getElementsByClassName('class-name') ... html elements don't have a .getElementBy... method attached to them我只是改变了你把element.getElementsByClassName('class-name')document.getElementsByClassName('class-name') ...... html 元素没有.getElementBy...方法附加到它们

function updateCartTotal() {
    var cartItemContainer = document.getElementsByClassName('cart-items')[0]
    var cartRows = document.getElementsByClassName('cart-row')

    var total = 0

    for (var i = 0; i < cartRows.length; i++) {
        var cartRow = cartRows[i]
        var priceElement = document.getElementsByClassName('cart-price')[0]
        var quantityElement = document.getElementsByClassName('cart-quantity-input')[0]
        var price = parseFloat(priceElement.innerText.replace('$', ''))
        var quantity = quantityElement.value
        total = total + (price * quantity)
    }

    document.getElementsByClassName('cart-total-price')[0].innerText = '$' + total



}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM