假设我有这段 HTML 代码:
<select id='list'>
<option value='1'>First</option>
<option value='2'>Second</option>
<option value='3'>Third </option>
</select>
<input type ="text" id="text"/>
然后是这段JavaScript代码
//other variables are also declared here
var x1 = 5;
var x2 = 10;
var value = x1 * x2;
var list_value =$("#list").change(function() {
$(this).val();
// just an example of how i want the function in the variable
});
var nwval = value * list_value;
$('#text').val(nwval);
// some long piece of code
// i'm also using the list_value value somewhere in the long piece of code..
我希望当用户选择选项时,文本框中的值能够改变,我知道如果把更改事件包装在它周围,它就会起作用,但是是否有任何方法可以在保留变量list_value
的同时完成这个操作?