admin管理员组文章数量:1287525
I want to change the value of value
using 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>
I want to change the value of value
using 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>
Share
Improve this question
edited Sep 24, 2011 at 10:00
Marek Sebera
40.7k37 gold badges164 silver badges249 bronze badges
asked Sep 24, 2011 at 9:28
no_freedomno_freedom
1,96110 gold badges30 silver badges48 bronze badges
10
- What is the problem? What doesn't work? – Pekka Commented Sep 24, 2011 at 9:29
-
2
The
alert
is executed before you make any change to the select field (at the time when you assignvalue = "test";
and defineupdatevariable
, not when you callupdatevariable
). Put it inside the event handler. – Felix Kling Commented Sep 24, 2011 at 9:31 - what is the issue. it seems to be working fine jsfiddle/JmpSU – Mridul Kashatria Commented Sep 24, 2011 at 9:32
-
@Pekka When I run this code. Its display alert
hello
. Not2/15
– no_freedom Commented Sep 24, 2011 at 9:32 - @mridkash it is not working on that fiddle, read question again – Marek Sebera Commented Sep 24, 2011 at 9:32
1 Answer
Reset to default 7You 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
本文标签: javascriptChange variable value using onchangeStack Overflow
版权声明:本文标题:javascript - Change variable value using onchange - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741239368a2363664.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论