Javascript确认返回发布值?

时间:2012-03-29 17:22:02

标签: javascript jsp post

尝试在帖子中设置标志以允许处理或不处理。 javascript对我不起作用我想弹出一个对话框,然后设置一个隐藏的帖子bool 0取消,1为ok。是否有更好的方法根据确认回报设置后期值?我搜索的所有东西都会带来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>

1 个答案:

答案 0 :(得分:3)

这部分:

 onSubmit="return confirmSubmit()"

应显示在<form>标记内,而不是<input submit>标记内。像这样:

<form onSubmit="return confirmSubmit()" name="delPlayers" method="post" action="deletePlayer.jsp" class="col6 leftpad3 rightpad3">

<强> [编辑]

你没有得到它的工作,因为你的javascript结构不如所需。我试图将你的代码改成这样的东西,事情似乎更好:

if (r){
    document.forms['delPlayers']["confirmed"].value = r;
    return true ;
}else{
    return false ;
}

在javascript中编写 if 命令时,请记住始终使用大括号{和}。你没有义务使用它们,但是避免像这样荒谬的问题是一种上帝的习惯。

技术说明:在JS中, if(condition) 没有卷曲的brakets {}只能使用仅1行命令它。在你的情况下,有2个命令,所以“其他”的声明是非法的。这就是为什么你总是应该使用大括号

的原因
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()
}
相关问题