我有一个表格,当我提交它时,我想保持在同一页面上。我只想处理发送到服务器的信息并保持在同一页面上。
为此,我有以下表格:
<form action='adicionaLoja' method='post'>
<input type='text' autocomplete='off' name='nome'/>
<button type='submit' id='saveStore' class='btn' value='Save'>Save</button>
<input type=hidden name='xValue'/><input type=hidden name='yValue'/></form>
这是我处理数据的地方:
@RequestMapping("adicionaLoja")
public void adiciona(Loja loja) {
System.out.println("adicionou - " + loja.getNome() + " X: " + loja.getxValue() + " Y: " + loja.getyValue());
}
使用此代码,当我点击提交按钮时,服务器会尝试重定向到 adicionaLoja.jsp。但是我没有创建这个文件,我只想点击提交按钮并停留在同一页面上。我试图返回创建表单的页面的名称,但是这样页面被重新加载,我也不想重新加载它,只想将请求发送到服务器。
我该怎么做?
编辑:我的 java 脚本文件:
var form = $('#submitLoja');
form.find('submit:first').click( function() {
$.ajax( {
type: "POST",
url: "adicionaLoja",
data: form.serialize(),
success: function( response ) {
console.log( response );
}
} );
} );
我的表单(请注意,此表单是在我声明提交点击事件的同一个 .js 文件中声明的):
<form action='adicionaLoja' id='submitLoja' method='post'><input type='text' autocomplete='off' name='nome'/>
<button type='submit' id='saveStore' class='btn' value='Save'>Save</button>
<input type=hidden name='xValue'/><input type=hidden name='yValue'/></form>
这是服务器端:
@RequestMapping("adicionaLoja")
@ResponseBody
public void adiciona(Loja loja) {
System.out.println("adicionou - " + loja.getNome() + " X: " + loja.getxValue() + " Y: " + loja.getyValue());
}
完整的 JavaScript 文件:
$(document).ready(function(){
$('.ImgMapa').popover({
html: true,
trigger: 'manual',
title: "<form id='submitLoja' method='post'><input type='text' autocomplete='off' data-provide='typeahead' data-items='4' name='nome' data-source='["Alabama","Alaska","Arizona","Arkansas","California","Colorado","Connecticut","Delaware","Florida","Georgia","Hawaii","Idaho","Illinois","Indiana","Iowa","Kansas","Kentucky","Louisiana","Maine","Maryland","Massachusetts","Michigan","Minnesota","Mississippi","Missouri","Montana","Nebraska","Nevada","New Hampshire","New Jersey","New Mexico","New York","North Dakota","North Carolina","Ohio","Oklahoma","Oregon","Pennsylvania","Rhode Island","South Carolina","South Dakota","Tennessee","Texas","Utah","Vermont","Virginia","Washington","West Virginia","Wisconsin","Wyoming"]'/>"
+ "<button type='submit' id='saveStore' class='btn' value='Save'>Save</button><input type=hidden name='xValue'/><input type=hidden name='yValue'/></form>"
});
$("#submitLoja").submit(function(event) {
alert("oie");
return false;
});
});
// Function to show popover on the image mouse click
$(function() {
var xMousePos = 0;
var yMousePos = 0;
var lastScrolledLeft = 0;
var lastScrolledTop = 0;
// Manages the page scrolling and add X and Y when the page is scrolled
$(window).scroll(function(event) {
if (lastScrolledLeft != $(document).scrollLeft()) {
xMousePos -= lastScrolledLeft;
lastScrolledLeft = $(document).scrollLeft();
xMousePos += lastScrolledLeft;
}
if (lastScrolledTop != $(document).scrollTop()) {
yMousePos -= lastScrolledTop;
lastScrolledTop = $(document).scrollTop();
yMousePos += lastScrolledTop;
}
});
function captureMousePosition(event) {
xMousePos = event.pageX;
yMousePos = event.pageY;
}
$('.ImgMapa').click(function(e) {
$(this).popover('show');//this is used to correct the popover location on the first time click
captureMousePosition(e);
var offset = $(this).offset();
var left = xMousePos;
var top = yMousePos;
var theHeight = $('.popover').height();
//http://jsfiddle.net/2EBGE/1/
$('.popover').css('cssText', 'left: ' + (left+10) + 'px !important; top: ' + (top-(theHeight/2)-10) + 'px !important');
$(this).popover('show');
$("input[name='xValue']").val(xMousePos);
$("input[name='yValue']").val(yMousePos);
});
// Hide popover when ESCAPE key is pressed
$(document).keydown(function(e) {
if (e.keyCode === 27)
$('.ImgMapa').popover('hide');
});
});