ReferenceError:赋值的左侧不是参考

我有一些参考错误的问题:


作业的左侧不是参考。


我想使用该代码只在 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());

    }


慕的地10843
浏览 142回答 2
2回答

摇曳的蔷薇

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

猛跑小猪

您正在尝试分配此处调用函数的结果:function onInput({target}) {&nbsp; target.val() = extractNouns(target.val()).join('\n');// -----^^^^^^^^^&nbsp; console.log(target.val());}你不能那样做。要设置输入的值,请将值传递给val函数:&nbsp; target.val(extractNouns(target.val()).join('\n'));请注意,您在该函数的参数列表中使用了解构:function onInput({target}) {// --------------^------^这将尝试target从传入的内容中挑选出一个属性,并为您提供该属性的值而不是传入的值。根据您调用它的方式,您不想在那里使用解构:function onInput(target) {
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript