jquery提交表单,然后在现有的div中显示结果 [英] jquery submit form and then show results in an existing div

查看:39
本文介绍了jquery提交表单,然后在现有的div中显示结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的文本输入表单,提交时需要获取一个 php 文件(将输入传递给文件),然后获取结果(仅一行文本)并将其放入 div 并将 div 淡入视图中.

I have a simple one text input form that when submitted, needs to fetch a php file (passing the inputs to the file) and then take the result (just a line of text) and place it in a div and fade that div into 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中.

What I need is the results of create.php?url=INPUT, to be dynamically loaded into the div called created.

我有 jquery 表单脚本,但我无法让它正常工作.但我确实加载了库(文件).

I have the jquery form script, but I haven't been able to get it to work right. But I do have the library loaded (the file).

推荐答案

这段代码应该可以做到.对于像这样简单的事情,您不需要 Form 插件:

This code should do it. You don't need the Form plugin for something as simple as this:

$('#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
});

这篇关于jquery提交表单,然后在现有的div中显示结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆