我有一个ArrayList<Product>
我希望在我的<script>
代码中访问其getter。它只是根本不起作用。我想知道是否有可能访问<script>
标签内的getter值。
JavaScript的:
var counter = 1;
var iteration1 = "productid" + counter;
var iteration2 = "orderquantity" + counter;
var totalPrice = 0;
function compute() {
while(document.getElementById(iteration1)[0] !== null && document.getElementById(iteration2)[0] !== null) {
var price = 0;
var isDone = false;
for(var i = 0; i < products.size() && isDone === false; i++) {
if(products.get(i).getProductID() === document.getElementById(iteration1)[0]) {
price = products.get(i).getProductPrice() * document.getElementById(iteration2)[0];
totalPrice += price;
isDone = true;
}
}
counter++;
iteration1 = "productid" + counter;
iteration2 = "orderquantity" + counter;
}
document.getElementById("totalPrice").innerHTML = totalPrice;
}
我还在考虑是否可以使用其他解决方案来访问其值,例如将其值放在<a id="iteration1">
或其他内容中。但我似乎无法找到解决方案,因为我的Java中的变量与我id=""
中的值不同。
<%
ArrayList<Product> products = (ArrayList<Product>)session.getAttribute("filteredproducts");
int counter = 1;
String iteration1 = "productid" + counter;
String iteration2 = "orderquantity" + counter;
for(Product p: products) {
%>
<tr>
<td>
<a id="iteration1"><%=p.getProductID()%></a>
</td>
<td>
<%=p.getProductLine()%> (Php <a id="iteration2"><%=p.getProductPrice()%></a>)
</td>
</tr>
<%}%>
答案 0 :(得分:1)
我终于在评论部分的上述链接的帮助下解决了我的问题。只是分享它,这是我使用的代码:
JavaScript的:
function compute() {
var counter = 1;
var iteration1 = "productid" + counter;
var iteration2 = "productquantity" + counter;
var totalPrice = 0;
var idArray = new Array();
var priceArray = new Array();
<%
for(Product p: products) {
%>
idArray.push("<%=p.getProductID()%>");
priceArray.push("<%=p.getProductPrice()%>");
<%}%>
for(var j = 0; j < click; j++) {
var isDone = false;
for(var i = 0; i < idArray.length && isDone === false; i++) {
if(idArray[i] === document.getElementById(iteration1).value) {
totalPrice += priceArray[i] * document.getElementById(iteration2).value;
isDone = true;
}
}
counter++;
iteration1 = "productid" + counter;
iteration2 = "productquantity" + counter;
}
document.getElementById("totalPrice").innerHTML = totalPrice;
}