我想调用一个返回值的javascript函数,然后将该值放在if语句中.HTML中有两个单选按钮,javascript会检查单击哪一个.之后,JSP将其与"客户"或"公司"进行比较,并执行相应的SQL查询.
使用Javascript:
function corc{
var value;
if(document.getElementById('cust').checked){
value='customer';
return value;
}else if(document.getElmentById('comp').checked){
value='company';
return value;
}
}
Run Code Online (Sandbox Code Playgroud)
JSP:
if(%>corc();<%.equals("customer")){
String sqlqueryCommand = "SELECT * from customer where login='" + v1 + "' and password='" + v2 + "'";
}else if (%>corc();<%.equals("company")){
String sqlqueryCommand = "SELECT * from company where login='" + v1 + "' and password='" + v2 + "'";
}
Run Code Online (Sandbox Code Playgroud)
Ani*_*rni 11
你不能在JSP的if语句中调用JavaScript函数,因为JSP是在服务器端执行的,而JavaScript是在客户端执行的.
您必须在单击单选按钮时触发事件,使用onclick
可以调用函数的事件corc()
.
不要在JSP中编写scriptlet,因为scriptlet不应该在JSP中使用超过十年.学习JSP EL,JSTL,并使用servlet作为Java代码.如何避免JSP-Files中的Java代码?
.......
........
//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
Run Code Online (Sandbox Code Playgroud)
onclick
事件我分配了handleClick
函数并传递this.id
,参数this.id
用于传递id
单击的单选按钮的属性.
<script type="text/javascript">
function handleClick(clickedId)
{
if(clickedId == "customerId")
document.getElementById('tableTextId').value = "customer";
else
document.getElementById('tableTextId').value = "company";
}
</script>
Run Code Online (Sandbox Code Playgroud)
String tableName = request.getParameter("tableValue"); //传递隐藏字段的名称,即tableValue
tableName
进行查询. 归档时间: |
|
查看次数: |
77630 次 |
最近记录: |