【问题标题】:How to check if atleast one checkbox is checked in my jsp?如何检查我的jsp中是否至少选中了一个复选框?
【发布时间】:2013-06-10 16:51:35
【问题描述】:

这是在我的资金表中显示所有详细信息的 jsp,然后提供一个用于删除任何一个的复选框。但现在我想添加一个 javascript 来检查是否选中了至少一个复选框,以便一个 db 调用不是浪费了……

<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Deleting Funds</title>
<script type="text/javascript">
var checkForm = function(form){
var inputs = form.getElementsByTagName('input');
for(var i = 0, l = inputs.length; i < l; i++){
    var input = inputs[i];
    if(input.type == "checkbox" && input.checked)
            return true;
    }
    alert('none are checked');
    return false;
};
</script>
</head>
<body>
<%
  Enumeration names = request.getParameterNames();
  while (names.hasMoreElements()) {
  String name = (String) names.nextElement();
  StringBuffer sb = new StringBuffer(name);
      com.mini.funds.Admin.Delete(sb.toString());
  }

%>
<br>

<div class="navigator">
<a href="index.html">Add</a>
<a id="currenttab" href="delete.jsp">Delete</a>
<a href="update.jsp">Update</a>
</div>

<br> <br> <br>

<form action="deleteFunds.jsp" method="post" onsumbit="return checkForm(this);">
<table>
<tr>
<th>code</th>
<th>name</th>
<th>type</th>
<th>nav</th>
<th>min_holdings</th>
<th>desription</th>
<th>terms</th>
<th>check</th>
</tr> 
<%

 ArrayList<MutualFund> al = com.mini.funds.Admin.GetFunds();
      String id;
  String box = null;
  int num = al.size();
  int i=0;
   while(i<num)
  {
MutualFund  almf = al.get(i);
out.print("<tr>");
      out.print("<td>");
      String code = almf.getMf_code();
      out.print(code);
      out.print("</td>");

      out.print("<td>");
     String x = almf.getMf_name();
      out.print(x);
      out.print("</td>");

      out.print("<td>");
      String y = almf.getMf_type();
     out.print(y);
     out.print("</td>");

     out.print("<td>");
     int a = almf.getNav();
    out.print(a);
    out.print("</td>")
    ;
    out.print("<td>");
     int b = almf.getMf_min_holdings();
   out.print(b);
   out.print("</td>");

       out.print("<td>");
       String z = almf.getMf_description();
      out.print(z);
      out.print("</td>");

      out.print("<td>");
      String l = almf.getMf_TandC();
     out.print(l);
     out.print("</td>");

  out.print("<td>");
  box = "<input name=" + code + " type='checkbox'>";
  out.print(box);
  out.print("</td>");
  out.print("</tr>");
  i++;
 }
%>

</table>

<br>
<input type="submit" value="Delete">
</form>
</body>
</html>

【问题讨论】:

    标签: javascript jsp checkbox


    【解决方案1】:

    您需要做的就是遍历表单中的所有输入标签,检查那些是复选框并选中的标签。如果找到,请照常提交表格。如果没有找到,请返回 false 取消提交表单。

    示例函数:

    var checkForm = function(form){
        var inputs = form.getElementsByTagName('input');
        for(var i = 0, l = inputs.length; i < l; i++){
            var input = inputs[i];
            if(input.type == "checkbox" && input.checked)
                return true;
        }
        alert('none are checked');
        return false;
    };
    

    并且可以在表单的onsubmit中调用:

    onsumbit="return checkForm(this);"
    

    演示:http://jsfiddle.net/QZPYG/

    【讨论】:

    • 没有出现警告框..这不适用于我的代码:( :(
    • 您确定在提交时调用了 checkForm 函数吗?
    • 我尝试了上面的代码.. 但不幸的是,如果我点击删除而不选择一个复选框,则不会显示警告框
    • 您是否在
      标签中添加了onsubmit="return checkForm(this)"
    • 它在提交时显示未定义的属性名称...我认为这是错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-21
    • 2012-04-11
    • 2011-02-10
    • 2013-02-21
    相关资源
    最近更新 更多