Javascript/JQuery:在表单提交时,不要重新加载整个页面(只有一个 div),并且仍然提交表单数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20819342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript/JQuery: On form submit, don't reload full page (only a div), and still submit form data
提问by kdjernigan
I've got the following code that I use on my links. This prevents the page from reloading and loads the content from the href tag in a div.
我在链接上使用了以下代码。这可以防止页面重新加载并从 div 中的 href 标记加载内容。
$("a[rel='right']").click(function(e){
e.preventDefault();
pageurl = $(this).attr('href');
$.ajax({url:pageurl.replace('index.php', 'rightcolumn.php')+'&rel=right',success: function(data){
$('#WMS_NEW_right').fadeOut(500, function(){ $('#WMS_NEW_right').html(data).fadeIn(1000); });
}
});
if(pageurl!=window.location){
window.history.pushState({path:pageurl},'',pageurl);
}
return false;
});
});
My Question:
I need the use the same concept behind this, except on form submit, it needs to not reload the page, but submit the form only inside a div #WMS_NEW_right
. How can I do this? I don't need push state or anything, just need to be able to control that form with class="formrelright"
to only reload a div and get the url from the form action. I will also need all data from the form method="POST"
on the new page (inside div)
我的问题:我需要在这背后使用相同的概念,除了在表单提交时,它不需要重新加载页面,而是只在 div 内提交表单#WMS_NEW_right
。我怎样才能做到这一点?我不需要推送状态或任何东西,只需要能够控制该表单,class="formrelright"
只需重新加载一个 div 并从表单操作中获取 url。我还需要method="POST"
新页面上表单中的所有数据(在 div 内)
回答by jrpotter
From my understanding, you want to use ajax to post a form without reloading the page during the form submission. So I would consider the following:
根据我的理解,您希望在表单提交期间使用 ajax 发布表单而不重新加载页面。所以我会考虑以下几点:
$('.formrelright').submit(function(event) {
event.preventDefault();
$.ajax({
url: url,
type: 'POST',
data: $(this).serialize(),
success: function(data) {
// Whatever you want
}
});
});
回答by Josh Beam
Maybe try it like this:
也许试试这样:
HTML:
HTML:
<!-- Notice there is no 'form' element -->
<div id="myform">
<input type="text" name="firstName"><br>
<input type="text" name="lastName"><br>
<button type="button" id="submit_myform">Submit</button>
</div>
<div id="resultArea"></div>
<!-- the biggest critique about this method might be that
yes, it is not very semantic. If you want to use a form,
just throw in an 'e.preventDefault()' in your jQuery -->
jQuery:
jQuery:
$('#submit_myform').on('click',function() {
var firstName = $('input[name="firstName"]').val(),
lastName = $('input[name="lastName"]').val();
$.ajax({
type: "POST",
url: 'form.php',
data: {
firstname: firstName,
lastname: lastName
},
//the success function is automatically passed the XHR response
success: function(data) {
$('#resultArea').html(data);
},
});
});
回答by Eran Boudjnah
Either use target and an IFrame, or JQuery to submit the form in the background. The latter is preferable if you want to use the contents of the response.
使用目标和 IFrame 或 JQuery 在后台提交表单。如果您想使用响应的内容,后者更可取。