如何将值从 javascript 传递到 struts2 中的 java 类?

2022-01-16 00:00:00 javascript java struts2 struts
function redirect(id){
alert(id);

document.forms["AddToCart"].submit();
}

这是我的 JavaScript.如何将id"的值传递给 AddToCart.java.我正在使用 struts2 框架.

This is my javascript. How can i pass the value of 'id' into AddToCart.java. I am using struts2 framework.

推荐答案

您可以将值存储在表单内的隐藏字段中,以便在提交表单时将值发送到 Action.

You can store the value in a hidden field inside your form and so when the form is submitted the value will be sent to Action.

<form name="AddToCart" ... >
...
<input type="hidden" id="myid"/>
.....
</form>

然后

function redirect(id){
document.getElementById('myid').value = id;

document.forms["AddToCart"].submit();
}

相关文章