嗨,我在使用 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/