Calling javascript function from a select onChange [duplicate]

假如想象 提交于 2019-12-10 03:39:39

问题


So I have a simple HTML select box, and a javascript alert function. I want the select box to have an onchange event that will call the javascript alert function. This is what I have so far:

HTML

<div style="float: left">Type: <select id="selector" onChange="jsfunction()">
  <option value="Pyr">Pyramidally</option>
  <option value="Lin">In-Lines</option>
  <option value="Sym">Symmetrically</option>
  <option value="Nsym">Non-Symmetrically</option>
</select>
</div>

Javascript

function jsfunction(){
    alert("hi");
}

It doesn't work and for the life of me I can't figure out why. Most other questions of this nature I've found suggest "" around calling the function - which I've got or making the onChange 'c' capital, which I also have. Any possible help? I'd be very grateful.


回答1:


<!DOCTYPE html>
<html>
    <head>
        <script>
            function jsfunction(){
                alert("hi");
            }
        </script>
    </head>
    <body>
        <select id="selector" onchange="jsfunction()">
            <option value="Pyr">Pyramidally</option>
            <option value="Lin">In-Lines</option>
            <option value="Sym">Symmetrically</option>
            <option value="Nsym">Non-Symmetrically</option>
        </select>
    </body>
</html>

It works. You made a mistake in onchange.



来源:https://stackoverflow.com/questions/24750766/calling-javascript-function-from-a-select-onchange

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!