mrp*_*atg 44 html javascript ajax jquery
我有一个简单的文本输入表单,在提交时,需要获取一个php文件(将输入传递给文件),然后获取结果(只是一行文本)并将其放入a div并淡入div到视图中.
这就是我现在拥有的:
<form id=create method=POST action=create.php>
<input type=text name=url>
<input type="submit" value="Create" />
<div id=created></div>
Run Code Online (Sandbox Code Playgroud)
我需要的是create.php?url=INPUT动态加载到被div调用者的结果created.
我有jquery表单脚本,但我无法让它正常工作.但我确实加载了库(文件).
Pao*_*ino 77
这段代码应该这样做.对于像这样简单的事情,您不需要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
});
Run Code Online (Sandbox Code Playgroud)
这也适用于文件上传
$(document).on("submit", "form", function(event)
{
event.preventDefault();
var url=$(this).attr("action");
$.ajax({
url: url,
type: 'POST',
dataType: "JSON",
data: new FormData(this),
processData: false,
contentType: false,
success: function (data, status)
{
$('#created').html(data); //content loads here
},
error: function (xhr, desc, err)
{
console.log("error");
}
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
91443 次 |
| 最近记录: |