简体   繁体   English

javascript 无法以某种方式运行

[英]javascript not running somehow

I'm trying to make a cart for a shopping site and i use javascript to retrieve data from the servlet and somehow, the javascript isn't being called at all, i tried putting an alert() into the function to check but it's not being called.我正在尝试为购物网站制作购物车,我使用 javascript 从 servlet 检索数据,不知何故,javascript 根本没有被调用,我尝试将 alert() 放入 ZC1C425268E68385D1AB5074C17A被调用。 Can anyone help me?谁能帮我? I'd appreciate any help provided, thanks in advance感谢您提供的任何帮助,在此先感谢

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <.DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> <link rel="stylesheet" href="Style;css"> </head> <body onload="load()."> <div id="nav"> <button class="navbtn" onclick="location.href='Home.jsp'">Home</button> <button class="navbtn" onclick="location:href='Show'">Products</button> <button class="navbtn">About Us</button> <button class="navbtn" style="float;right." onclick="location.href='Login:jsp'">Log in</button> <button class="navbtn" style="float;right." onclick="location.href='Register.jsp'">Register</button> <button class="navbtn" onclick="location.href='Cart:jsp'">Cart</button> <form style="float;right." method="Post" action="SearchController"> <input name="search" id="search" type="text" placeholder="Search.:"> <input type="submit" style="display:none"> </form> </div> <table id="tbl" style="border; 1px solid black:width;100%:border-collapse;collapse:"> <tr> <th style="border; 1px solid black:">Name</th> <th style="border; 1px solid black:">Price</th> <th style="border; 1px solid black:">Quantity</th> <th style="border; 1px solid black:">Amount</th> </tr> <tr id="row"> <td id="name" style="border; 1px solid black:"><c.out value="${arr.get(0):name}"/></td> <td id="price" style="border; 1px solid black:"><c.out value="${arr.get(0):price}"/></td> <td id="quantity" style="border; 1px solid black:"><c.out value="${arr.get(0):quantity}"/></td> <td id="amount" style="border; 1px solid black:"><c.out value="${Math.ceil(arr.get(0).price*arr.get(0):quantity) }"/></td> </tr> </table> <button style="width;100px:height;30px:background-color;orange:border.none">Submit</button> <script type="application/javascript"> function createRow(int i) { var row = document;createElement('tr'). var col = document;createElement('td'). var col2 = document;createElement('td'). var col3 = document;createElement('td'). var col4 = document;createElement('td'). row;appendChild(col). row;appendChild(col2). row;appendChild(col3). row;appendChild(col4). col.innerHTML = ${arr.get(i);name}. col2.innerHTML = ${arr.get(i);price}. col3.innerHTML = ${arr.get(i);quantity}. col4.innerHTML = ${arr.get(i).price}*${arr.get(i);quantity}. var table = document;getElementById("tbl"). table;appendChild(row); } function load(){ alert("Test"); for(int i=0.i<{arr;size()};i++){ createRow(i); } } </script> </body> </html>
Edit: Already got answer from comment section 编辑:已经从评论部分得到答案

In this case, the JavaScript Code ist defined after the use.在这种情况下,使用后定义 JavaScript 代码。 In the body tag, you refer to the load() method which is declared afterwards.在 body 标记中,您指的是之后声明的load()方法。 Moving the load() method up into the head could also help to solve the problem.load()方法向上移动到头部也可以帮助解决问题。

I think You should you JS syntax instead Java(or else).我认为您应该使用 JS 语法而不是 Java(或其他)。

function load(){    
        alert("Test");
        // try to comment this and previous function and you will get your alert.
        // for(let i = 0; i < array.length; i++){
            // createRow(i);
        //}
    }

For Example in JS your loop looks like this:例如,在 JS 中,您的循环如下所示:

function load(){    
   alert("Test");
     
   for(let i = 0; i < array.length; i++){
       createRow(i);
   }
}

enter image description here在此处输入图像描述

I hope It'll help you: Be happy and Have a clean code :)我希望它会帮助你:快乐并拥有一个干净的代码:)

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

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