gpt4 book ai didi

jQuery:如何在不关闭 jQuery 对话框的情况下提交表单

转载 作者:行者123 更新时间:2023-11-28 01:42:25 27 4
gpt4 key购买 nike

我在 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com