简体   繁体   English

如何使用带有变量的Javascript / Jquery刷新特定的div

[英]How to refresh specific div using Javascript/Jquery with the variables on it

I have a variable $salary which is dynamic. 我有一个变量$ salary ,这是动态的。 How can I refresh specific div every 5seconds. 如何每隔5秒刷新一次特定的div

index.html 的index.html

<html>
    <body>
        <img src="<?php echo $filename.'.jpg'; ?>" />
        <p id="salary"><?php echo $salary; ?></p>
    </body>
</html>

Is there any javascript/jquery way to refresh #salary only. 是否有任何javascript / jquery方法只刷新#salary Please help.. 请帮忙..

You can execute an ajax call: 您可以执行ajax调用:

function ajaxCall() {
    $.ajax({
        method: 'POST',
        url: 'path/to/asyncHalndler.php',
        data: { ... },
        success: function (data) {
            if (data) {
                $('#salary').html(data);
            }
        }
    });
}

// Execute ajax call every 5 seconds
setInterval(function() {
    ajaxCall();
},5000);
var salary = document.getElementById('salary');

Now, the value of your variable salary will be the DOM node where you would like to change the value. 现在,您的变量薪水的值将是您想要更改值的DOM节点。 When you get new data, you can refresh text inside your p tag with salary id by adding salary.innerText = yourNewValue; 当您获得新数据时,可以通过添加salary.innerText = yourNewValue;来刷新p标签中带有工资ID的salary.innerText = yourNewValue;

This is pure JavaScript way of updating that element with id. 这是使用id更新该元素的纯JavaScript方式。

You will need an jquery ajax call for that. 你需要一个jquery ajax调用。 first you should create php file get_salary.php where you send id from jquery ajax if you want to update the salary for unique id: 首先你应该创建php文件get_salary.php ,如果你想更新唯一id的工资,你可以从jquery ajax发送id:

in get_salary.php you need to get salary from database so the code in this php file will be like that 在get_salary.php中你需要从数据库中获得薪水,所以这个php文件中的代码就是这样的

$id = $_POST['ID']
$query = mysql_query("SELECT * FROM sallaries WHERE id='$id'") or die("Can't connect");
$fetch = mysql_fetch_array($query)
$salary = $fetch['salary']
echo $salary

after that you will need javascript file(eg script.js) from where you will send the request and id to the get_salary.php and grab the data from it, after that you will be able to update salary in html, so code in javascript file will be like that: 在那之后你将需要javascript文件(例如script.js)从那里你将请求和id发送到get_salary.php并从中获取数据,之后你将能够更新html中的工资,所以javascript中的代码文件将是这样的:

function updateSalary(){}
var id=25;
$.ajax({
    url: "get_salary.php",
    type: 'POST',

    //sending id
    data:'id='+id,
    success: function(result){

        //updating html
        $("#salary").html(result);
    }
});
}

//setting interval to update in every second
setInterval(updateSalary, 1000)

so it will update your salary in the div 所以它会在div中更新你的工资

It's better to use ajax way. 最好使用ajax方式。 But if u are looking for a very simple solution then jQuery .load will be the best 但如果你正在寻找一个非常简单的解决方案,那么jQuery .load将是最好的

setInterval($("#salary").load("<url to get the value>"), 1000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM