我有这个代码:
$(document).ready(function() {
var totalHours = 0;
$('select').each(function(sel)
{
totalHours += parseFloat($(sel).val());
});
alert(totalHours);
});
所以我希望看到 <select>
的值的总和页面上的元素,但我在 jQuery 中收到此错误(它发生在 each
循环内的行上)。为什么我会收到此错误,如何修复它?
最佳答案
$().each
https://api.jquery.com/each/
Type:
Function( Integer index, Element element )
A function to execute for each matched element.
函数签名是(index, element)
,但您将其用作(element)
- 更改为
$('select').each(function(i, sel)
更新的代码:
var totalHours = 0;
$('select').each(function(i, sel) {
totalHours += parseFloat($(sel).val());
});
console.log(totalHours);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select>
<option value="1">1</option>
</select>
关于javascript - jQuery 内部错误 : Uncaught TypeError: Cannot read property 'toLowerCase' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60191256/