我有一个脚本,它从 php 加载一些 div id,并且每 x 秒重新加载新值。我想在脚本内实现 onchange 值,以便触发加载新变量(如果(在本例中艺术家更改))但我不知道如何.. 脚本是:
setInterval(function(){
cache: false,
$("#artist").load("test.php #artist");
$("#song").load("test.php #song");
}, 2000);
我需要判断艺术家是否更改,然后使用我在此处找到的包装器代码从 php 加载新变量 How can I make a program wait for a variable change in 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);
关于Javascript 从 php 和 onchange 变量加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19889706/