试图在帖子中设置一个允许或不允许处理的标志。javascript对我不起作用,我想弹出一个对话框,然后设置一个隐藏的post bool 0表示cancelled,1表示ok。有没有更好的方法可以根据confirm返回设置post值?我搜索的所有内容都会显示ASP.NET及其postBack值。
表单是使用简单的JSP生成的(我知道它已经过时了,JSTL更适合我的需求):
<form name="delPlayers" method="post" action="deletePlayer.jsp" class="col6 leftpad3 rightpad3">
<input type="hidden" name="confirmed" value="0" />
<select name="playerName">
<% while (results.next())
{
out.print("<option value="");
out.print(results.getString("username"));
out.print("">");
out.print(results.getString("username"));
out.print("</option>");
} %>
</select>
<input type="submit" name="Submit" class="button" value="Submit" onSubmit="return confirmSubmit()" />
</form>
我是如何在发布之前检查确认返回并设置值的。
<script type="text/javascript">
<!--
function confirmSubmit()
{
var r = confirm("Remove " + document.forms['delPlayers']["playerName"].value + "?");
if (r)
document.forms['delPlayers']["confirmed"].value = r;
return true ;
else
return false ;
}
-->
</script>
本部分:
onSubmit="return confirmSubmit()"
应显示在<form>
标记内,而不是<input submit>
标记内。像这样:
<form onSubmit="return confirmSubmit()" name="delPlayers" method="post" action="deletePlayer.jsp" class="col6 leftpad3 rightpad3">
[EDIT]
您没有让它工作,因为您的javascript结构没有所需的那么好。我试着把你的代码改成这样,情况似乎更好了:
if (r){
document.forms['delPlayers']["confirmed"].value = r;
return true ;
}else{
return false ;
}
请记住,在javascript中编写if命令时,始终使用大括号{和}。你没有义务使用它们,但避免这样荒谬的问题是上帝的习惯。
技术说明:在JS中,不带卷曲制动器{}的if(condition)
仅在其后面的一行命令下工作。在您的案例中,有2个命令,所以"else"语句是非法的。这就是为什么你总是应该使用if和花括号的原因
if (condition)
foo()
else
bar()
可以。但是
if (condition)
foo()
any_extra_command();
else
bar()
将导致错误。所以最好的情况是:
if (condition){
foo();
any_extra_command();
......
anything_you_want_else();
}else{
bar()
}