javascript - 输入文本按时间间隔重新加载

标签 javascript php jquery html ajax

嗨,我在使用 PHP 和 ajax 每 15 秒重新加载一个 div 时遇到了问题。它工作正常,但我唯一想知道的是是否可以加载输入文本,并且当间隔刷新 div 时不要刷新输入文本,因为有时当您在输入中写入时,它只会重新加载,并且您得再写一次。

这是代码的一部分: 文件 1,我通过 ajax 将内容加载到 div 中:

<script type="text/javascript">// <![CDATA[
$(document).ready(function() {
$.ajaxSetup({ cache: false }); // This part addresses an IE bug.  without it, IE will only load the first number and will never refresh
setInterval(function() {
$('#precio').load('valoractual.php');
}, 15000); // the "3000" here refers to the time to refresh the div.  it is in milliseconds. 
});
// ]]></script>

这是重新加载的表单:

 <p>Current Bid:  <?php echo "USD ".$puja; ?></p>
                                              <p><form id="bid" name="bid"> 
                                              <input type="text" name="puja" placeholder="<?php $sobrepujar = $puja + 500; echo $sobrepujar; ?>" id="puja" size="14" /><input type="hidden" id="usuario" value="<?php echo $_SESSION['idusuario']; ?>" /><input type="hidden" id="valoractual" value="<?php echo $puja; ?>" /><input type="hidden" id="idpuja" value="<?php echo $idpuja; ?>" /> <input type="button" id="submit" value="B I D"/>
              </form>

提前致谢!

最佳答案

仅当 #puja 元素在页面上不可用或(它没有焦点和空值)时才(重新)加载页面:

<script type="text/javascript">// <![CDATA[
$(document).ready(function() {
$.ajaxSetup({ cache: false }); // This part addresses an IE bug.  without it, IE will only load the first number and will never refresh
setInterval(function() {

if($("#puja").length == 0 || (!$("#puja").is(":focus") && $("#puja").val() == "")) {

    $('#precio').load('valoractual.php');
}

}, 15000); // the "3000" here refers to the time to refresh the div.  it is in milliseconds. 
});
// ]]></script>

关于javascript - 输入文本按时间间隔重新加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26639546/

相关文章:

javascript - Jquery ui 如何选择那些启用的选项卡?

javascript - 深色背景中的 c3 图;如何更改轴和刻度值颜色

javascript - 如何在一个选择器 jQuery 中显示和隐藏多个元素?

PHP 5.5 类名解析

javascript - 单击时强制悬停状态辅助元素

javascript - Nodejs 异步更新插入问题

javascript - 如何访问在复制 webpack 插件期间散列的静态资源的实际名称/路径/url

javascript - Jquery div 淡入淡出

php - 在匿名 PHP 函数中从父范围访问变量

php - img src 不适用于 php 和 php 变量