dongshi2141 2018-08-24 13:08
浏览 61

从选择框中调用JS函数

I am using MVC pattern for building a PHP movie web. in View class, I print out the select box that contain an array.

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>
    ";

The JS function movieFilterChanged() will be triggered when value was selected. I wrote the simple JS function :

function movieFilterChanged(){
    var actor = document.getElementById('id_actor').value;
    if (actor != 'all')
        alert("actor");
}

But this JS function is not triggered.

  • 写回答

1条回答 默认 最新

  • douwen5584 2018-08-24 13:16
    关注

    Firts change the id from your select:

    id='id_actor'
    

    Try use alert without quotation marks:

    alert(actor);
    

    And you can add a else in your if, like this:

    else {
     alert(actor);
    }
    

    OR use:

    console.log(actor);
    

    You will see the value of the actor and you will notice that you are using a different id

    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧