未捕获的TypeError: Cannot read property '1'未定义的,使用value作

Uncaught TypeError: Cannot read property '1' of undefined using value as array key

本文关键字:未定义 使用 value property TypeError read Cannot      更新时间:2023-09-26

我想改变名为websitemod的输入值,我想把tab[var][1]的值放进去,其中var根据从下拉菜单中选择的选项动态更改。但是,每次我把var val = $(this).val(); tab[val][1],我有一个错误(Uncaught TypeError: Cannot read property '1' of undefined),当我做var = 12; tab[val][1],它的工作。我假设我为val赋值的jQuery函数不起作用。你有什么主意吗?

函数本身:

$(document).ready(function () {
    // everytime the dropdown changes
    $('select[name=modservmenu]').change(function () {
        var val = $(this).val();
       // changes the value of the input
       $('input[name=websitemod]').val(tab[val][1]);
    });
})

标签,如果你需要的话:

var tab = [
        ['10', ''],
        ['4', ''],
        ['8', ''],
        ['9', ''],
        ['11', ''],
        ['3', ''],
        ['2', ''],
        ['1', ''],
        ['6', ''],
        ['5', ''],
        ['7', ''],
        ['12', 'test.fr'],
        ];
编辑:

改变了在评论中建议的数组,与parseInt它做的事情我猜

var tab = {
     '10' : [''],
     '4' : [''],
     '8' : [''],
     '9' : [''],
     '11' : [''],
     '3' : [''],
     '2' : [''],
     '1' : [''],
     '6' : [''],
     '5' : [''],
     '7' : [''],
     '12' : ['test.fr'],
};

您在这里缺少的是,您不能通过单元格的单元格值访问数组的单元格。此外,你的"val"是一个字符串,但"tab"是一个数组,所以它使用索引和索引是整数。你可以通过它的索引来访问array,比如array[0]。所以你对"indexes" <--> "values"感到困惑

具体来说,假设你想获得元素"12",你像array[12]那样访问它,但它的索引是"11",所以你可以通过它的索引访问它,就像array[11] ->给你"["12","test.fr"]那样,然后你可以使用这个数组对象。示例:

//let's say value is 12
tab["12"] --> null
//let's get the array contains value "12"
tab[11] --> ["12", "test.fr"],
//let's access to "test.fr"
tab[11][1] --> "test.fr"

并且,这个空对象的索引"1"是未定义的,所以你得到了你的错误"Uncaught TypeError: Cannot read property '1' of undefined"

tab["12"][1] //--> Uncaught TypeError: Cannot read property '1' of undefined

你的tab对象不是一个映射,所以你不能通过"字符串"值访问它。所以你必须创建一个像下面这样的对象来通过文本索引访问它然后,你可以访问数组索引(任何你想要的索引):

var tab = {
        '10' : ['', ''],
        '4': ['', ''],
        '8': ['', ''],
        '9': ['', ''],
        '11': ['', ''],
        '3': ['', ''],
        '2': ['', ''],
        '1': ['', ''],
        '6': ['', ''],
        '5': ['', ''],
        '7': ['', ''],
        '12': ['', 'test.fr'],
        };
$(document).ready(function () {
    // everytime the dropdown changes
    $('select[name=modservmenu]').change(function () {
        var val = $(this).val();
       // changes the value of the input
       $('input[name=websitemod]').val(tab[val][1]);
    });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="modservmenu">
  <option>10</option>
  <option>12</option>
</select>
<input type="text" name="websitemod"/>

或者,您可以使用您的数组并像这样搜索它:

var tab = [
        ['10', ''],
        ['4', ''],
        ['8', ''],
        ['9', ''],
        ['11', ''],
        ['3', ''],
        ['2', ''],
        ['1', ''],
        ['6', ''],
        ['5', ''],
        ['7', ''],
        ['12', 'test.fr'],
        ];
$(document).ready(function () {
    // everytime the dropdown changes
    $('select[name=modservmenu]').change(function () {
        var val = $(this).val();
  
        for (var i = 0; i < tab.length; i++) {
          if (tab[i][0] == val) {
            $('input[name=websitemod]').val(tab[i][1]);
            break;
          }
        }
    });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="modservmenu">
  <option>10</option>
  <option>12</option>
</select>
<input type="text" name="websitemod"/>

你在评论中说:

下拉菜单中使用的值来自数据库中自动递增的ID。目前是1到12。

好的,所以这意味着我猜对了,你不能使用val直接索引到你的tab数组。相反,您需要在tab数组中找到以val作为第一个参数的条目。幸运的是,数组有这样的东西:find:

var entry = tab.find(function(e) { return e[0] == val; });

请注意,Array#find是相对较新的,但可以在旧的浏览器中填充。

var tab = [
  ['10', ''],
  ['4', ''],
  ['8', ''],
  ['9', ''],
  ['11', ''],
  ['3', ''],
  ['2', ''],
  ['1', ''],
  ['6', ''],
  ['5', ''],
  ['7', ''],
  ['12', 'test.fr'],
];
$(document).ready(function() {
  // everytime the dropdown changes
  $('select[name=modservmenu]').change(function() {
    var val = $(this).val();
    // changes the value of the input
    var entry = tab.find(function(e) { return e[0] == val; });
    if (entry) {
      $('input[name=websitemod]').val(entry[1]);
    }
  });
})
<select name="modservmenu">
  <option value="2">Two</option>
  <option value="12">Twelve</option>
</select>
<input name="websitemod">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

你不应该使用'var'作为变量名,它是javascript中保留的关键字(也许它不在你的脚本中,只是在你的帖子中)

检查数组键的类型。

tab[12][1]

可能与

不同
tab['12'][1]

也许你只是需要

var val = parseInt($(this).val());