Variables 更改变量onchange select而不重新加载页面

Variables 更改变量onchange select而不重新加载页面,variables,select,option,onchange,Variables,Select,Option,Onchange,当我更改选项时,如何更改所选选项中variable:var_number的值。并且页面不能像使用submit时那样重新加载 <form name="form1" method="POST"> <select name="select1" id="select1_id" > <option value="2" >2</option> <option value="15" >15</option> </select>

当我更改选项时,如何更改所选选项中variable:var_number的值。并且页面不能像使用submit时那样重新加载

<form name="form1" method="POST">
<select name="select1" id="select1_id" >
<option value="2" >2</option>
<option value="15" >15</option>
</select>
</form>
document.write(var_number)

2.
15
文件写入(变量编号)

使用onchange事件处理程序(或Javascript库中的类似程序)更新变量

例如:

<select name="select1" onchange="updatevariable()">

然后可以创建一个Javascript函数来更新变量


使用document.write也不是一个好主意,除非这只是为了测试-使用innerHTML或类似的东西。

好,现在我使用下面的代码。所选选项位于变量“val”中。但是现在我想更改div id。那么如何将'var_div'的值更改为'val'的值呢?2 15函数updatevariable(elm){val=elm.options[elm.selectedIndex].value;window.alert(val);}我在你的新主题中回答了这个问题。