如何在使用输入字段时防止刷新

时间:2013-04-04 07:03:45

标签: jquery

我在jquery中使用了以下函数来刷新div但是每10秒刷新一次,当我在该div中写任何内容时也会刷新。所以,任何人都可以给我解决方案

setTimeout(function () {
   $('.div').load('url');
}, 1000); // refresh every 1000 milliseconds

在jquery中运行,但在输入字段中编写任何思考时出现问题。

2 个答案:

答案 0 :(得分:0)

编辑:啊我刚看到你的编辑。我不太明白你的问题。 一世 这是你的问题吗? http://board.phpbuilder.com/showthread.php?10305727-Keep-form-input-after-a-refresh-reload

最好使用Jquery AJAX刷新你的div。

在这个网站上你可以找到一个很好的教程。 http://www.jquery4u.com/tutorials/auto-refresh-div-content-jquery-ajax/

这是一个有效的演示: http://www.jquery4u.com/demos/auto-refresh-div-content/

一个例子:

<html>
<head>
<title>Auto Refresh Div Content Demo | jQuery4u</title>
<!-- For ease i'm just using a JQuery version hosted by JQuery- you can download any version and link to it locally -->
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
(function($)
{
    $(document).ready(function()
    {
        $.ajaxSetup(
        {
            cache: false,
            beforeSend: function() {
                $('#content').hide();
                $('#loading').show();
            },
            complete: function() {
                $('#loading').hide();
                $('#content').show();
            },
            success: function() {
                $('#loading').hide();
                $('#content').show();
            }
        });
        var $container = $("#content");
        $container.load("rss-feed-data.php");
        var refreshId = setInterval(function()
        {
            $container.load('rss-feed-data.php');
        }, 9000);
    });
})(jQuery);
</script>
</head>
<body>

<div id="wrapper">
    <div id="content"></div>
    <img src="loading.gif" id="loading" alt="loading" style="display:none;" />
</div>

</body>
</html>
希望它有所帮助。

答案 1 :(得分:0)

iddin你编码用div类刷新所有标签(.div表示类名'div')

你可以为你的div设置一个id,并使用它:

<div id="update-div"></div>

然后:

setTimeout(function () {
   $('#update-div').load('url')}, 1000); // refresh every 1000 milliseconds
}