您不能在JSP的if语句中调用Javascript函数,因为JSP在服务器端执行,而Javascript在客户端执行。
单击单选按钮之一时必须触发事件,使用
onclick
事件可以调用functioncorc()
。不要在JSP中编写scriptlet,因为scriptlet在JSP中不应使用超过十年。学习JSPEL和JSTL,并将servlet用于Java代码。
JSP代码:
...............//use <form> to submit values to servlet <input type="radio" name="radio1" onclick="handleClick(this.id);" id="customerId" /> <input type="radio" name="radio1" onclick="handleClick(this.id);" id="companyId" />.............//use hidden field to assign table value i.e. "customer" or "company". <input type="hidden" name="tablevalue" id="tableTextId" /> //</form> closing form tag
onclick事件我分配了
handleClick函数并传递了
this.id参数
this.id,用于传递
id单击的单选按钮的属性。
Javascript代码:
<script type="text/javascript"> function handleClick(clickedId) { if(clickedId == "customerId") document.getElementById('tableTextId').value = "customer"; else document.getElementById('tableTextId').value = "company"; }</script>- 当您提交表单然后在servlet中时,您可以获得隐藏字段的值。
字符串tableName = request.getParameter(“ tablevalue”); //传递隐藏字段的名称,即tablevalue
- 您可以进一步将其传递
tableName
给查询。



