为了账号安全,请及时绑定邮箱和手机立即绑定

ReferenceError:赋值的左侧不是参考

ReferenceError:赋值的左侧不是参考

慕的地10843 2021-06-14 16:17:26
我有一些参考错误的问题:作业的左侧不是参考。我想使用该代码只在 textarea 中输入这些 regexNounFilters。$('#toFile').on('keypress', function() {    onInput($('#toFile'));});const regexNounFilters = [<?php        $data = $pdo->query("SELECT PRODUCT_CODE AS code, ENDING AS ec FROM test")->fetchAll(PDO::FETCH_OBJ);        foreach ($data as $key) {            $separator = ($key != end($data)) ? ", " : '';            $std = "/^(" . $key->code . ")([a-zA-Z0-9]{" . $key->ec . "})$/";            echo $std.$separator;        }    ?>];    const extractNouns = string =>      string      .split('\n')      .filter(line =>        regexNounFilters.some(re =>          line.trim().toUpperCase().match(re)        )    );    function onInput({target}) {      target.val() = extractNouns(target.val()).join('\n'); // error points here...      console.log(target.val());    }
查看完整描述

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())

}


查看完整回答
反对 回复 2021-06-18
?
猛跑小猪

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) {


查看完整回答
反对 回复 2021-06-18
  • 2 回答
  • 0 关注
  • 141 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信