Как отправить данные с помощью метода click в другой HTML-файл при выполнении функции? - PullRequest
0 голосов
/ 07 ноября 2019

Я хочу отправить содержимое тега раздела из Первого файла во второй файл, используя метод и функцию jQuery, выполнение которых находится внутри этого метода, определение этой функции находится в отдельном файле, как показано ниже. Я получил Uncaught TypeError: Невозможно прочитать свойство 'push' из null - я не знаю, где моя ошибка.

first HTML

cart = [];
class Item {
    constructor(name, price, count){
        this.name = name;
        this.price = price;
        this.count = count;
    };
} 
addItemToCart = function(name,price,count){
    for (let i in cart) {
        if ((cart[i]).name === name){
            (cart[i]).count += count;
            return;
        }
    }
    let item = new Item(name,price,count);
    (cart).push(item);
    $("#show-cart").html(cart)
}
saveCart = function(){
    localStorage.setItem("cart",JSON.stringify(cart));
}
loadCart = function(){
    cart = JSON.parse(localStorage.getItem("cart")); 
}
loadCart();
<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
        <link rel="stylesheet" href="shopcart.css"> 
    </head>
    <body>
        <section class="a" id="title">Relax in the morning</section>
        <section class="a" id="price">100$</section>
        <form id="form1" runat="server">
            <div>
                <input type="button" id="btnClick" value="Submit" onclick="addItemToCart(title,price,1);" />
            </div>
        </form>
        <script>

            $("section").click(function(event){
                event.preventDefault();

                const name = $(this).attr("#title");
                const price = Number($(this).attr("#price"));

                localStorage.setItem("title",JSON.stringify(name));
                localStorage.setItem("price", JSON.stringify(price));
                
                addItemToCart(name,price,1);


            });

        </script>
        <script src="main.js"></script>
    </body>
</html>

<html>
    <head>
        <title> Shopping Cart</title>
        <link rel="stylesheet" href="shopcart.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    </head>
    <body>
        <main>
            <div id="show-cart"></div>


        </main>

        <footer>
        </footer>

        <script src="main.js">
            const title = JSON.parse(localStorage.getItem("title"));
            const price = JSON.parse(localStorage.getItem("price"));
        </script>

    </body>    
</html>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...