ajax添加

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="webjars/jquery/2.2.4/jquery.js"></script>
<script type="text/javascript">
    $(function(){
        $.post(
            "slist",
            function(obj){
                for(var i in obj){
                    $("[name='sid']").append("<option value='"+obj[i].sid+"'>"+obj[i].sname+"</option>")
                }
            },"json"
        
        )
    })
    function add(){
        $.post(
                "add",
                $("form").serialize(),
                function(obj){
                    if(obj){
                        alert("添加成功");
                        location.href="list";
                    }
                },"json"
            
            )
    }
</script>
</head>
<body>
<form>
   <table>
     
      <tr>
         <td>班级
          <select name="sid">
          
         </select>
         </td>
      </tr>
      <tr>
         <td>名称
           <input type="text" name="name">
         </td>
      </tr>
    
      <tr>
         <td>
           <input type="button" onclick="add()" value="提交">
         </td>
      </tr>
   </table>
</form>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/uyhbrr/article/details/88664738