javascript - 如果变量发生变化则触发事件

标签 javascript jquery html

我目前有一个 div,显示 DNS 和 IP 地址等信息。

我试图在 IP 地址变量发生更改时触发警报,因此我尝试监听 div 和变量本身的更改,但运气不佳。如果变量发生变化,是否有明显的方法来提醒或触发函数?

 $(ipv4.ipaddr).change(function() {
        alert("IP Changed");
    }); 

以及用于正常显示IP地址的脚本:

if(result.ipv4){
    $("#btn-disconnect").show();
    $("#btn-disconnect-modem").show();
    var ipv4=result.ipv4;
    html+="<tr><td>IPAddr</td><td>"+ ipv4.ipaddr+"</td></tr>";
    if(ipv4.netmask) html+="<tr><td>Netmask</td><td>"+ ipv4.netmask+"</td></tr>";
    if(ipv4.gateway) html+="<tr><td>Gateway</td><td>"+ ipv4.gateway+"</td></tr>";
    label_dns="DNS";
    for(var i=0;i<ipv4.dns.length;i++){
        html+="<tr><td>"+label_dns+"</td><td>"+ ipv4.dns[i] +"</td></tr>";
        label_dns="";
    }
    if(proto=="wwan" || proto=="tethering"||proto=="3g"){
        if(result.tx) html+="<tr><td>TX Data</td><td>"+(result.tx/1024/1024>1? ((result.tx/1024/1024).toFixed(2)+" MB"): ((result.tx/1024).toFixed(2) +" KB") )+" </td></tr>";
        if(result.rx) html+="<tr><td>RX Data</td><td>"+(result.rx/1024/1024>1? ((result.rx/1024/1024).toFixed(2)+" MB"): ((result.rx/1024).toFixed(2) +" KB") )+"</td></tr>";
    }
    if(typeof sta_connected === "function") sta_connected();

}else{
    if( (proto=="wwan" ||proto=="tethering")){
        if(!result.disabled) html+="<tr><td>Connecting to the Internet</td></tr>";
    }
    if(proto=="wisp" ||proto=="wds"){
        if(!result.disabled) html+="<tr><td>Connecting to the Internet</td></tr>";
        result.disabled? $("#btn-disconnect").hide():$("#btn-disconnect").show();
    }
}
$(section).html(html);
}

最佳答案

我需要 html 来给出更好的答案,但我给你的这个已经可以工作了!我想你使用 jQuery 并且你必须知道何时要检查 ip,我的意思是你可以总是在主体上放置一个 onclick 方法(不推荐),或者当用户按下任何可用的提交按钮时可以这样做形式(我将解释这一形式)。看看这个:

<script>

if($("input=[type=submit]").on('click',function(){

  var lastip=document.getElementById("ip").value;
  $.post("yourfile.php", { lastip: lastip }, function(data){
    $("#yourdiv").html(data);
  });

 });

</script>

yourfile.php

   function user_ip() {
        $ip = '';
        if (getenv('HTTP_CLIENT_IP'))
            $ip = getenv('HTTP_CLIENT_IP');
        else if(getenv('HTTP_X_FORWARDED_FOR'))
            $ip = getenv('HTTP_X_FORWARDED_FOR');
        else if(getenv('HTTP_X_FORWARDED'))
            $ip = getenv('HTTP_X_FORWARDED');
        else if(getenv('HTTP_FORWARDED_FOR'))
            $ip = getenv('HTTP_FORWARDED_FOR');
        else if(getenv('HTTP_FORWARDED'))
           $ip = getenv('HTTP_FORWARDED');
        else if(getenv('REMOTE_ADDR'))
            $ip = getenv('REMOTE_ADDR');
        else
            $ip = 'Not Available';
        return $ip;
    }

$lastip= $_POST['lastip'];
$nowip= user_ip();

if($nowip!=$lastip AND !empty($lastip)){

echo 'ip changed!';
exit();

}

 <div id="yourdiv">
    <table>
      <tr>
        <td id="ip">IP:<?php echo $nowip;?></td>
        <td id="dns">DNS:</td>
      </tr>
    </table>
 </div>

关于javascript - 如果变量发生变化则触发事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48009927/

相关文章:

javascript - 如何向此弹出代码添加一次性功能?

javascript - 如何在jquery插件中从json而不是html标签获取数据?

c# - 如何在JavaScript中设置asp.net隐藏字段并在后面的C#代码中访问该值

html - 我的 webpack 没有生成我的 css 文件

javascript - 为什么我不能在 HTML5 中用另一个空 Canvas 清除 Canvas ?

javascript - Chrome 无法通过 javascript 重定向 URL

php:我如何从多个下拉列表中将值插入数据库并添加一个文本

javascript - 如何将不同的样式应用于 bootstrap 下拉列表(移动 View 和 pc View 时)

javascript - 从 jquery 中的另一个 javascript 调用函数

javascript - 在node.js中返回多个shell命令的结果