使用在jquery的另一个函数中定义的变量

时间:2018-12-25 09:13:16

标签: javascript jquery

我有一个Range Slider,我想将其随每次更改放入变量中,然后使用它。 在我的代码中,这是未定义的

我的html代码是

<input id="length" class="border-0" type="range" value="10" min="max="100"/>

我的jquery代码是

$('#length').change(function () {
    length = $(this).val();
    console.log($(this).val())
});
console.log(length)

但它会返回未防御状态

1 个答案:

答案 0 :(得分:1)

在脚本中包含JQuery库将使其正常工作。

var length;
$('#length').change(function () {
    length = $(this).val();
    console.log($(this).val())
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="length" class="border-0" type="range" value="10" min="max="100"/>