我有一个简单的文本输入表单,提交时需要获取一个 php 文件(将输入传递给文件),然后获取结果(只是一行文本)并将其放入 div
并将 div
淡入 View 。
这是我现在拥有的:
<form id=create method=POST action=create.php>
<input type=text name=url>
<input type="submit" value="Create" />
<div id=created></div>
我需要的是 create.php?url=INPUT
的结果,动态加载到名为 created
的 div
中。
我有 jquery 表单脚本,但我无法让它正常工作。但我确实加载了库(文件)。
最佳答案
这段代码应该可以做到。对于像这样简单的事情,您不需要 Form 插件:
$('#create').submit(function() { // catch the form's submit event
$.ajax({ // create an AJAX call...
data: $(this).serialize(), // get the form data
type: $(this).attr('method'), // GET or POST
url: $(this).attr('action'), // the file to call
success: function(response) { // on success..
$('#created').html(response); // update the DIV
}
});
return false; // cancel original event to prevent form submitting
});
关于javascript - jquery 提交表单,然后在现有的 div 中显示结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1218245/