从 php 和 onchange 变量加载 JavaScript

Javascript load from php and onchange variable

本文关键字:加载 JavaScript 变量 onchange php      更新时间:2023-09-26

我有一个脚本,可以从php加载一些div id,每x秒重新加载新值。我想在脚本中实现一个 onchange 值,以便在(在此示例中艺术家更改)触发加载新变量,但我无法弄清楚如何......脚本为:

setInterval(function(){
    cache: false,
    $("#artist").load("test.php #artist");
    $("#song").load("test.php #song");
}, 2000);

我需要告诉艺术家是否更改然后从 php 加载一个新变量,使用我在这里找到的这个包装器代码如何让程序等待 javascript 中的变量更改?

function Wrapper(callback) {
    var value;
    this.set = function(v) {
        value = v;
        callback(this);
    }
    this.get = function() {
        return value;
    }  
} 

如果我对输入框使用相同的示例:<input type="text" onchange="wrapper.set(this.value)"/>它有效..但是我不知道如何使用第一部分中从 php 中提取的 #artist 使其工作。

我试过这样做:

<script type="text/javascript">
    setInterval(function(){
        cache: false,
        $("#artist").load("test.php #artist").onchange(wrapper.set(this.value));
        $("#song").load("test.php #song");
    }, 2000);
</script>

和许多其他组合,但都不起作用。你能帮帮我吗!

PS:请记住,我是javascript的初学者。

谢谢你的回答。

setInterval(function(){
    cache: false,
    $.get('test.php',function(data){
     var artist = $('<div>' + data + '</div>').find('div#artist').html();
     if(artist != $('#artist').html()){
          $('#artist').html(artist );
          wrapper.set(artist)
    } //in here I assumed #artist is a div or span,... if its a textbox change it to $('#artist').val()
    });
    $("#song").load("test.php #song");
}, 2000);