当我在文本框中输入值并点击提交时,我希望使用ajax在文本框下方输入该值
When i enter value into textbox and hit submit i want that value below the textbox using ajax
Welcome.jsp
这是我的jsp页面。当我在文本框中输入一个值并点击回车键时,我希望名称与<p>
标记一起显示。我是ajax的新手。请帮我做这件事,我们可以把模型发送到ajax吗?
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<title>Insert title here</title>
</head>
<script>
function ajaxCall()
{
//alert("in js");
$.ajax({
type: "GET",
url: "/welcomePage",
data: "name=" + $('#name').val(),
success: function(data)
{
alert("success !");
$('#show').html(data);
},
error: function()
{
alert("Error");
}
});
return false;
}
</script>
<body>
<h2 align="center">Welcome to Spring MVC</h2>
<form>
<input type="text" name="name" />
<input type="submit" value="submit" onclick="ajaxCall();"/>
</form>
<p>Your name is</p><div id="show"></div>
</body>
</html>
DemoController.java
package com.demo.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class DemoController {
@RequestMapping(value="/welcomePage" , method=RequestMethod.GET)
public @ResponseBody String showPage(
HttpServletRequest request,
HttpServletResponse response)
{
String name = request.getParameter("name");
/*ModelAndView model = new ModelAndView("Welcome");
model.addObject("name",name);
System.out.println("Name is:"+name);*/
return name;
}
}
表单中的提交类型按钮将表单提交到其操作url,如果未指定url,则会重新加载当前url。当表单提交当前页面上下文(DOM、CSS、JS)被破坏时,简而言之,当表单提交时,对当前页面无法执行任何操作。
你所需要做的就是阻止表单提交
要做到这一点,您可以将false返回到它的onsubmit事件,如下面的
<form onsubmit="return false;">
<input type="text" name="name" />
<input type="submit" value="submit" onclick="ajaxCall();"/>
</form>
从外观上看,其他一切都是正确的,所以在你做出这个简单的改变之后,它应该会起作用。
相关文章:
- 让文本输入幻灯片显示输入时的新文本输入?然后向后滑动
- Sails.js:同时发布文本输入和一个文件
- 来自文本输入null的html javascript变量
- 当没有文本输入聚焦时检测空格键按下
- JS中的按钮和文本输入
- JavaScript:在调用函数的文本输入上按enter键
- jQuery自动完成功能不适用于多个文本输入
- 使用JS从选择和文本输入中捕获值,并将输出返回到HTML
- 如何通过jQuery发送选定的元素和文本输入
- HTML如何根据javascript函数的返回值限制文本输入
- jquery插件或javascript方法自动调整文本输入(而非文本区域)(固定宽度)可变高度的大小
- 在jquery中获取文本输入val始终为空
- HTML文本输入大小
- 文本输入与文本区域
- 角度ui选择标记模糊时丢失文本输入
- 文本输入值中的JavaScript变量
- Angular指令中的最佳实践是将文本输入设置为英尺和英寸的格式
- 如何在不通过模型验证的情况下屏蔽文本输入中输入的字符
- 在文本输入区域中创建双向更新
- 不使用自定义CSS或HTML(使用框架方法)的角度材质文本输入或文本区域标签大小