作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 jQuery 对话框中有一个 html 表单。
目前,当我单击表单中的“添加”按钮(其作用类似于“提交”,但只是添加了一些数据检查机制)时,jQuery 对话框将关闭。
但是用户可能想要添加更多的项目,所以我不想在点击“添加”按钮后关闭对话框。
以下是代码片段。
<head>
<script>
$(document).ready(function(){
$( "#add_post" ).dialog({
autoOpen: false,
width: 600
});
$( "#add_post" ).click(function() {
$( "#add_data" ).dialog( "open" );
});
$('#postForm').submit(function() {
return false;
});
});
</script>
</head>
<body>
<script>
function checkData() {
// always return true now
return true;
}
function submitData() {
if (checkData()) {
document.getElementById("postForm").submit();
}
}
</script>
<input id="add_post" type="button" value="Add Post" />
<form id="postForm" name="postForm" action="add_something" method="post">
<input type="hidden" name="postUser" value="User"/>
<input id="add_button" type="button" value="add" onclick="submitData()"/>
</form>
</body>
我读过另一篇关于不通过添加以下内容关闭对话框的文章
$('#postForm').submit(function() {
return false;
});
但是好像不行。
有人有什么想法吗?
提前致谢。
埃里克
最佳答案
你可以使用 jquery 的 ajax。
$('#postForm').submit(function() {
var data = $(this).serializeArray();
var formURL = $(this).attr("action");
$.ajax({
url : formURL,
type: "POST",
data : data,
success:function(result){
$("#div1").html(result);
}});
e.preventDefault();
e.unbind(); //stop multiple form submit.
});
另一种选择是不使用 <form>
只需使用按钮
$('#button').onClick(function() {
var data = "name="+$("#txtName").val()+"&age="+$("#txtAge").val();
$.ajax({
url : "formController",
type: "POST",
data : data,
success:function(result){
$("#div1").html(result);
}});
});
关于jQuery:如何在不关闭 jQuery 对话框的情况下提交表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25111256/
我是一名优秀的程序员,十分优秀!