11 javascript jquery
假设我有这个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"/>
Run Code Online (Sandbox Code Playgroud)
然后是这个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..
Run Code Online (Sandbox Code Playgroud)
我希望文本框中的val能够随着用户选择一个选项而改变,我知道如果我将更改事件包装在它周围它会起作用,但是有什么办法可以将它保持为变量list_value吗?
Man*_*ijn 16
var x1 = 5;
var x2 = 10;
var value = x1 * x2;
var nwval;
$("#list").change(function() {
nwval = value * $(this).val();
$('#text').val(nwval);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
64869 次 |
| 最近记录: |