javascript - jquery 提交表单,然后在现有的 div 中显示结果

标签 javascript jquery html ajax

我有一个简单的文本输入表单,提交时需要获取一个 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 的结果,动态加载到名为 createddiv 中。

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

相关文章:

javascript - 点击后警报按钮的内部内容

javascript express js 传递异步结果

jquery - CSS: z-index 同时使用 float: left in image gallery

javascript - 为什么我的函数 .show of jquery 不能在按钮上工作

javascript - 在 HTML 或其他 XML 中嵌入 XML(带有 XML 声明)

html - 不透明度在 IE 中不起作用?

javascript - 使用 forEach 循环循环遍历一系列数字,并将任何包含整数 1 的数字替换为 "Beep"

javascript - 将 jQuery.on(event,selector,func) 转换为 .addEventListener(event, func) 特别是 DOM 树后面的选择器部分和事件气泡

html - 如何在html中向上移动一个按钮

javascript - javascript 中的变量范围 - 事件监听器未拾取