【发布时间】:2018-04-17 13:42:34
【问题描述】:
很难将 Jquery 地图函数转换为箭头函数。
这里是fiddle。
// find elements
$('button').click(function(e) {
let answerArray = $(`[name="foo"]`).map(function() {
return $(this).val();
}).get();
console.log(answerArray)
let answerArray2 = $(`[name="foo"]`).map(x => $(x).val()).get();
console.log(answerArray2)
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input name="foo">
<input name="foo">
<button>
enter
</button>
但我收到了。
VM118 jquery.js:7977 Uncaught TypeError: Cannot read property 未定义的“toLowerCase” 在 jQuery.fn.init.val (VM102 jquery.js:7977) 在 HTMLInputElement.$.map.x ((index):74) 在 VM102 jquery.js:194 在 Function.map (VM102 jquery.js:443) 在 jQuery.fn.init.map (VM102 jquery.js:193) 在 HTMLButtonElement。 ((指数):74) 在 HTMLButtonElement.dispatch (VM102 jquery.js:5183) 在 HTMLButtonElement.elemData.handle (VM102 jquery.js:4991)
【问题讨论】:
-
Array.from($('[name="foo"]'), x => x.value)
标签: javascript jquery ecmascript-6 arrow-functions