我正在使用MVC模式构建PHP电影网站。 在View类中,我打印出包含数组的选择框。
print "
<div>
<div class='top-nav'>
<label for='actor'>Actor:</label>
<select name='actor' id='id-actor' onchange='movieFilterChanged();'>
<option value='all'> Select All </option>
";
foreach($actors as $actor){
print "
<option value='".$actor['actor_id']."'> ".$actor['actor_name']." </option>
";
}
print "
</select>
</div>
";
选择值时,将触发JS函数movieFilterChanged()
。我写了简单的JS函数:
function movieFilterChanged(){
var actor = document.getElementById('id_actor').value;
if (actor != 'all')
alert("actor");
}
但是此JS函数未触发。
答案 0 :(得分:1)
装备从您的选择中更改 id :
id='id_actor'
尝试使用不带引号的警报
:alert(actor);
您可以在if中添加一个else,如下所示:
else {
alert(actor);
}
或使用:
console.log(actor);
您将看到演员的价值,并且您会发现自己是 使用其他ID