输入文本重新加载间隔

时间:2014-10-29 20:01:52

标签: javascript php jquery html ajax

您好我遇到了一个div,我每隔15秒用PHP和ajax重新加载一次。它工作正常,但我唯一想知道的是,是否可以加载输入文本,当间隔刷新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>

先谢谢!

1 个答案:

答案 0 :(得分:1)

(重新)只有当#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>