我是ajax的新手,我试图在没有刷新页面的情况下使用ajax和jstl插入数据库中的数据。 插入数据时它可以很好地工作,但它会跳转到动作网址而不是停留在同一页面上。 这是我的代码 HTML代码
<form action="insert.jsp?themesCat=1" method="post" name="themecat" id="themecat">
<div id="success" style="display:none" class="form-group has-success">
<label class="control-label" for="inputSuccess"><img src="images/admin/upthumb.png"/> One Theme Added Successfully...!!!</label>
</div>
<div class="form-group">
<label>Enter Theme Name:</label>
<input class="form-control" placeholder="Enter Name" type="text" name="themeCatName" id="themeCatName" required="true" title="Enter Thneme Name"/>
</div>
<div class="form-group"></div>
<button type="submit" class="btn btn-default">Submit Button</button>
<button type="reset" class="btn btn-primary">Reset Button</button>
</form>
AJAX代码
<script type="text/javascript">
var themecat = $('#themecat');
form.submit(function () {
$.ajax({
type: form.attr('method'),
url: form.attr('action'),
data: form.serialize(),
success: function () {
$('#success').fadeOut(200).hide();
}
});
return false;
});
</script>
INSERT.JSP
<c:if test="${not empty param.themesCat}">
<c:if test="${not empty param.themeCatName}">
<c:set var="max" value="1000" />
<sql:query var="res">Select tID from themes</sql:query>
<c:forEach items="${res.rows}" var="row">
<c:if test="${max < row.tID}">
<c:set var="max" value="${row.tID}" />
</c:if>
</c:forEach>
<sql:update var="n">
Insert into themes
values (
${max + 1},
'${param.themeCatName}'
)
</sql:update>
</c:if> success</c:if>
答案 0 :(得分:0)