javascript 无需重新加载页面即可提交表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18169933/
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
Submit form without reloading page
提问by Butterflycode
I have a function built in JavaScript that I want to be executed after a form submit is hit. It basically changes the look of the page completely. But I need a variable from the search box to still go through to the JavaScript. At the moment it flashes and resets what was there because it reloads the page.
我有一个内置于 JavaScript 的函数,我想在点击表单提交后执行该函数。它基本上完全改变了页面的外观。但是我需要一个来自搜索框中的变量来继续访问 JavaScript。目前它闪烁并重置那里的内容,因为它重新加载了页面。
So I set up a return false in my function which keeps it from doing that but the variable I want doesn't get submitted through the form. Any ideas on what I should do to get it? It's okay for the page to refresh as long as the updateTable()
function works and isn't reset by the page reset.
所以我在我的函数中设置了一个 return false 以防止它这样做,但我想要的变量没有通过表单提交。关于我应该怎么做才能得到它的任何想法?页面刷新是可以的,只要该updateTable()
功能正常工作并且没有被页面重置所重置。
<form action="" method="get" onsubmit="return updateTable();">
<input name="search" type="text">
<input type="submit" value="Search" >
</form>
This is the updateTable
function:
这是updateTable
函数:
function updateTable() {
var photoViewer = document.getElementById('photoViewer');
var photo = document.getElementById('photo1').href;
var numOfPics = 5;
var columns = 3;
var rows = Math.ceil(numOfPics/columns);
var content="";
var count=0;
content = "<table class='photoViewer' id='photoViewer'>";
for (r = 0; r < rows; r++) {
content +="<tr>";
for (c = 0; c < columns; c++) {
count++;
if(count == numOfPics) break; // check if number of cells is equal number of pictures to stop
content +="<td><a href='"+photo+"' id='photo1'><img class='photo' src='"+photo+"' alt='Photo'></a><p>City View</p></td>";
}
content +="</tr>";
}
content += "</table>";
photoViewer.innerHTML = content;
}
回答by Matt Bryant
You can't do this using forms the normal way. Instead, you want to use AJAX.
您不能以正常方式使用表单来执行此操作。相反,您想使用 AJAX。
A sample function that will submit the data and alert the page response.
将提交数据并提醒页面响应的示例函数。
function submitForm() {
var http = new XMLHttpRequest();
http.open("POST", "<<whereverTheFormIsGoing>>", true);
http.setRequestHeader("Content-type","application/x-www-form-urlencoded");
var params = "search=" + <<get search value>>; // probably use document.getElementById(...).value
http.send(params);
http.onload = function() {
alert(http.responseText);
}
}
回答by Ahsan Shah
You can use jQuery serialize function along with get/post as follows:
您可以将 jQuery 序列化函数与 get/post 一起使用,如下所示:
$.get('server.php?' + $('#theForm').serialize())
$.post('server.php', $('#theform').serialize())
jQuery Serialize Documentation: http://api.jquery.com/serialize/
jQuery 序列化文档:http: //api.jquery.com/serialize/
Simple AJAX submit using jQuery:
使用 jQuery 的简单 AJAX 提交:
// this is the id of the submit button
$("#submitButtonId").click(function() {
var url = "path/to/your/script.php"; // the script where you handle the form input.
$.ajax({
type: "POST",
url: url,
data: $("#idForm").serialize(), // serializes the form's elements.
success: function(data)
{
alert(data); // show response from the php script.
}
});
return false; // avoid to execute the actual submit of the form.
});
回答by SarathSprakash
I guess this is what you need. Try this .
我想这就是你需要的。试试这个 。
<form action="" method="get">
<input name="search" type="text">
<input type="button" value="Search" onclick="return updateTable();">
</form>
and your javascript code is the same
和你的javascript代码是一样的
function updateTable()
{
var photoViewer = document.getElementById('photoViewer');
var photo = document.getElementById('photo1').href;
var numOfPics = 5;
var columns = 3;
var rows = Math.ceil(numOfPics/columns);
var content="";
var count=0;
content = "<table class='photoViewer' id='photoViewer'>";
for (r = 0; r < rows; r++) {
content +="<tr>";
for (c = 0; c < columns; c++) {
count++;
if(count == numOfPics)break; // here is check if number of cells equal Number of Pictures to stop
content +="<td><a href='"+photo+"' id='photo1'><img class='photo' src='"+photo+"' alt='Photo'></a><p>City View</p></td>";
}
content +="</tr>";
}
content += "</table>";
photoViewer.innerHTML = content;
}
回答by Butterflycode
I did it a different way to what I was wanting to do...gave me the result I needed. I chose not to submit the form, rather just get the value of the text field and use it in the javascript and then reset the text field. Sorry if I bothered anyone with this question.
我以一种与我想做的不同的方式去做……给了我需要的结果。我选择不提交表单,而只是获取文本字段的值并在 javascript 中使用它,然后重置文本字段。对不起,如果我用这个问题打扰了任何人。
Basically just did this:
基本上就是这样做的:
var search = document.getElementById('search').value;
document.getElementById('search').value = "";