开发者

Change variable value using onchange

开发者 https://www.devze.com 2023-04-07 10:28 出处:网络
I want to change the value of value usin开发者_如何学Pythong a change event listener. Is it possible? Here is my sample code:

I want to change the value of value usin开发者_如何学Pythong a change event listener. Is it possible? Here is my sample code:

<select name="select1" onchange="updatevariable(this.value)"> 
    <option value="2" >2</option>
<option value="15" >15</option> 
</select>
<script type="text/javascript">
    value = "test";
    function updatevariable(data) { 
        value = data;       
    }
    alert(value); // It should be 2/15
</script>


You have alert() in wrong place

<select name="select1" onchange="updatevariable(this.value)"> 
    <option value="2" >2</option>
    <option value="15" >15</option> 
 </select>
 <script type="text/javascript">
    var value = "test";
    function updatevariable(data) { 
        value = data;
        alert(value);
    } 
 </script>

In your code it is loaded right after the script is loaded,
you have to modify it to be called right after change

0

精彩评论

暂无评论...
验证码 换一张
取 消