想要在提交表格后刷新div? [英] Want to refresh a div after submit the form?

查看:88
本文介绍了想要在提交表格后刷新div?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的表单和一个< div> 容器。我的问题是,当我提交表单时,我正在尝试刷新div。

I have a simple form and a <div> container. My problem is I'm trying to refresh the div when i submit the form.

<div id="refresh">
   FOO FOO FOOOOOOOOOOOOOOOOO
</DIV>

 <form  id="test">
    <input type="text" name"testFI">
    <input type="submit" id="postB"  value="Post" />
 </form>

任何帮助都将不胜感激!

Any help would be appreciated !

推荐答案

您可以使用 jQuery post(),一种使用HTTP POST请求从服务器加载数据的AJAX简写方法。

You can use jQuery's post(), an AJAX shorthand method to load data from the server using a HTTP POST request.

以下是使用ajax发布表单并将结果放入div 的示例:

<form action="/" id="searchForm">
    <input type="text" name="s" placeholder="Search...">
    <input type="submit" value="Search">
</form>

<!-- the result of the search will be rendered inside this div -->
<div id="result"></div>

<script>
    // Attach a submit handler to the form
    $( "#searchForm" ).submit(function( event ) {

        // Stop form from submitting normally
        event.preventDefault();

        // Get some values from elements on the page:
        var $form = $( this ),
            term = $form.find( "input[name='s']" ).val(),
            url = $form.attr( "action" );

        // Send the data using post
        var posting = $.post( url, { s: term } );

        // Put the results in a div
        posting.done(function( data ) {
            var content = $( data ).find( "#content" );
            $( "#result" ).empty().append( content );
        });
    });
</script>

这篇关于想要在提交表格后刷新div?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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