如何将jquery代码链接到jsp代码以显示表单?

时间:2015-09-30 12:56:54

标签: jquery jsp



"

$(function(){

 // Setup form validation on the #register-form element
    $("#loginform").validate({
    
        // Specify the validation rules
        rules: 
        {
            uid: "required",
            password:
            {
                required: true,
                minlength: 5
            },
            confirmpassword:
            {
                required: true,
                minlength: 5,
                equalTo: "#password"
            },
    		role: "required",
        },
        
        // Specify the validation error messages
        messages: 
        {
        	uid: "Please enter your user ID",
            password: 
            {
			    required: "Please provide a password",
			    minlength: "Your password must be at least 5 character",
            },
            confirmpassword: 
             {
 			    required: "Please provide a password",
 			    minlength: "Your password must be at least 5 character",
 			    equalTo: "your password should be same as above"
             },
            role: "Please specify your role",
        },
        	onClick: function(loginform) 
        	{
        		loginform.submit();
            }

      });

  });
       
 




我有这个jsp页面来显示表单。我在其他jsp文件中包含了jquery库。但是这个jquery代码不起作用。我无法在浏览器上进行客户端验证。并且提交按钮代码不起作用。请帮助我。

1 个答案:

答案 0 :(得分:0)

要提交你的,可以试试这个

function validate() {
        var flag = false;
        flag = $("#loginForm").valid();

        if (flag) {
            document.loginForm.action = "page1.jsp";
            document.loginForm.submit();
        }
    }

这里.valid()默认由jquery.validat.js提供,返回true或false

编辑: 这是我试过的最简单的演示

的index.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@ include file="include.jsp" %>

<!--    <div id="body"> -->
<%--     This is page 1. Rendering time: <%=new java.util.Date()%> --%> 
<!--    </div-->
<html>
<!-- <head> -->
<!--    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> -->
<!-- </head> -->
<body>
 <div>
<form role="form" id="form">
                                    <div class="form-group"><label>Password</label> <input type="password" placeholder="Password" class="form-control" name="password"></div>
                                    <div class="form-group"><label>Url</label> <input type="text" placeholder="Enter email" class="form-control" name="url"></div>
                                    <div class="form-group"><label>Number</label> <input type="text" placeholder="Enter email" class="form-control" name="number"></div>
                                    <div class="form-group"><label>MinLength</label> <input type="text" placeholder="Enter email" class="form-control" name="min"></div>
                                    <div class="form-group"><label>MaxLength</label> <input type="text" placeholder="Enter email" class="form-control" name="max"></div>
                                    <div>
                                        <button class="btn btn-sm btn-primary m-t-n-xs" type="submit"><strong>Submit</strong></button>
                                    </div>
                                </form>

</div>

</body>
</html>

include.jsp

 <script src="jquery-2.1.1.js"></script>
    <script src="jquery.validate.min.js"></script>
<script type="text/javascript">

  $(document).ready(function(){

      $("#form").validate({
          rules: {
              password: {
                  required: true,
                  minlength: 3
              },
              url: {
                  required: true,
                  url: true
              },
              number: {
                  required: true,
                  number: true
              },
              min: {
                  required: true,
                  minlength: 6
              },
              max: {
                  required: true,
                  maxlength: 4
              }
          }
      });
 });
</script>
  </script>