2 回答

TA贡献1793条经验 获得超6个赞
问题在这里:
target.val() = extractNouns(target.val()).join('\n')
您不能分配给函数调用的结果。
您可能希望该行是:
target.val(extractNouns(target.val()).join('\n'))
有关详细信息,请参阅jQueryval()文档。
另一个问题
您正在传递已更改的元素,然后target通过解构访问该元素上的属性。
$('#toFile').on('keypress', function() {
onInput($('#toFile')) // <- passing the element here
})
function onInput({target}) { // <- destructuring that element to get the target
target.val(extractNouns(target.val()).join('\n'))
console.log(target.val())
}
你要么想要:
$('#toFile').on('keypress', function() {
onInput($('#toFile')) // <- passing the element here
})
function onInput(target) { // <- directly accessing it
target.val(extractNouns(target.val()).join('\n'))
console.log(target.val())
}
或者
$('#toFile').on('keypress', function(e) {
onInput(e) // <- passing the error args here
})
function onInput({target}) { // <- destructuring to get the target
target.val(extractNouns(target.val()).join('\n'))
console.log(target.val())
}

TA贡献1858条经验 获得超8个赞
您正在尝试分配此处调用函数的结果:
function onInput({target}) {
target.val() = extractNouns(target.val()).join('\n');
// -----^^^^^^^^^
console.log(target.val());
}
你不能那样做。要设置输入的值,请将值传递给val函数:
target.val(extractNouns(target.val()).join('\n'));
请注意,您在该函数的参数列表中使用了解构:
function onInput({target}) {
// --------------^------^
这将尝试target从传入的内容中挑选出一个属性,并为您提供该属性的值而不是传入的值。根据您调用它的方式,您不想在那里使用解构:
function onInput(target) {
添加回答
举报