为什么我的变量表数据返回未定义?

我在代码中尝试执行的操作是在单击表时返回表的一行。我在 jquery 函数中执行此操作$("tr.table").click(function)....。之后,我尝试将此表行的数据存储在名为 tableData 的变量中。这一切都工作正常,但是当我尝试在 if 语句中使用变量 tabledata 时。我收到一个表数据未定义的错误。我的问题是如何在 if 语句中使用变量 tabledata 而不会出现错误。


我的 JavaScript 代码


function onUpdate() {

    var tableData

    var auth2 = gapi.auth2.getAuthInstance();

    var profile = auth2.currentUser.get().getBasicProfile();


    $("tr.table").click(function () {

        tableData = $(this).children("td").map(function () {

            return $(this).text();

        }).get();

    });


    if( tableData[7] === 2) {

        console.log("if statement");

        ...

    }

    else {

        console.log("else statement");

        $.confirm({

            title: '',

            content: '',

            buttons: {

                confirm: function () {


                }

            }

        });

    }

}

我的html表格代码


<table class="table table-bordered table-responsive-sm table-hover">

    <thead>

        <tr>

            <th>Date</th>

            <th>TimeSlot</th>

            <th>Room</th>

            <th>Attendees</th>

            <th>update</th>

            <th>delete</th>

        </tr>

    </thead>

         <tbody>

            <tr class="table">

                <td class="table">...</td>

                <td class="table">...</td>

                <td class="table">...</td>

                <td class="table">...</td>

                <td class="table" style="display: none">...</td>

                <td class="table" style="display: none">...</td>

                <td class="table" style="display: none">...</td>

                <td class="table" style="display: none">...</td>

                <td class="table command">

                    <a onclick="onUpdate()" style="color: #007bff; cursor: pointer"> <i class="fa fa-pencil-square-o" aria-hidden="true"></i></a>

                </td>



不负相思意
浏览 143回答 4
4回答

慕容708150

您不需要在onUpdate函数内分配点击事件。如果我理解正确,您想要单击更新按钮并执行if..else块。a您可以像 HTML 一样传递元素的引用onclick="onUpdate(this)"。然后使用.closest()查找tr并将整行作为数组获取。这里要保留的一件事是,if用于===比较可能不起作用,因为数组甚至对于数字也将包含字符串值。因此,要么进行类型转换,要么仅用于==比较此处所做的值。function onUpdate($this) {&nbsp; var tableData;&nbsp; tableData = $($this).closest('tr').children("td").map(function() {&nbsp; &nbsp; return $(this).text();&nbsp; }).get();&nbsp; //console.log(tableData);&nbsp; if (tableData[7] == 2) {&nbsp; &nbsp; console.log("if statement 2");&nbsp; } else if (tableData[7] == 22) {&nbsp; &nbsp; console.log("if statement 22");&nbsp; } else {&nbsp; &nbsp; console.log("else statement");&nbsp; }}<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script><table class="table table-bordered table-responsive-sm table-hover">&nbsp; <thead>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <th>Date</th>&nbsp; &nbsp; &nbsp; <th>TimeSlot</th>&nbsp; &nbsp; &nbsp; <th>Room</th>&nbsp; &nbsp; &nbsp; <th>Attendees</th>&nbsp; &nbsp; &nbsp; <th>update</th>&nbsp; &nbsp; &nbsp; <th>delete</th>&nbsp; &nbsp; </tr>&nbsp; </thead>&nbsp; <tbody>&nbsp; &nbsp; <tr class="table">&nbsp; &nbsp; &nbsp; <td class="table">...</td>&nbsp; &nbsp; &nbsp; <td class="table">...</td>&nbsp; &nbsp; &nbsp; <td class="table">...</td>&nbsp; &nbsp; &nbsp; <td class="table">value 2</td>&nbsp; &nbsp; &nbsp; <td class="table" style="display: none">...</td>&nbsp; &nbsp; &nbsp; <td class="table" style="display: none">1</td>&nbsp; &nbsp; &nbsp; <td class="table" style="display: none">3</td>&nbsp; &nbsp; &nbsp; <td class="table" style="display: none">2</td>&nbsp; &nbsp; &nbsp; <td class="table command">&nbsp; &nbsp; &nbsp; &nbsp; <a onclick="onUpdate(this)" style="color: #007bff; cursor: pointer"> update</a>&nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; <td class="table command">&nbsp; &nbsp; &nbsp; &nbsp; <a onclick="onDelete()" style="color: #007bff; cursor: pointer"> delete</a>&nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr class="table">&nbsp; &nbsp; &nbsp; <td class="table">...</td>&nbsp; &nbsp; &nbsp; <td class="table">...</td>&nbsp; &nbsp; &nbsp; <td class="table">...</td>&nbsp; &nbsp; &nbsp; <td class="table">value 22</td>&nbsp; &nbsp; &nbsp; <td class="table" style="display: none">...</td>&nbsp; &nbsp; &nbsp; <td class="table" style="display: none">1</td>&nbsp; &nbsp; &nbsp; <td class="table" style="display: none">3</td>&nbsp; &nbsp; &nbsp; <td class="table" style="display: none">22</td>&nbsp; &nbsp; &nbsp; <td class="table command">&nbsp; &nbsp; &nbsp; &nbsp; <a onclick="onUpdate(this)" style="color: #007bff; cursor: pointer"> update</a>&nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; <td class="table command">&nbsp; &nbsp; &nbsp; &nbsp; <a onclick="onDelete()" style="color: #007bff; cursor: pointer"> delete</a>&nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr class="table">&nbsp; &nbsp; &nbsp; <td class="table">...</td>&nbsp; &nbsp; &nbsp; <td class="table">...</td>&nbsp; &nbsp; &nbsp; <td class="table">...</td>&nbsp; &nbsp; &nbsp; <td class="table">value 33</td>&nbsp; &nbsp; &nbsp; <td class="table" style="display: none">...</td>&nbsp; &nbsp; &nbsp; <td class="table" style="display: none">1</td>&nbsp; &nbsp; &nbsp; <td class="table" style="display: none">3</td>&nbsp; &nbsp; &nbsp; <td class="table" style="display: none">33</td>&nbsp; &nbsp; &nbsp; <td class="table command">&nbsp; &nbsp; &nbsp; &nbsp; <a onclick="onUpdate(this)" style="color: #007bff; cursor: pointer"> update</a>&nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; <td class="table command">&nbsp; &nbsp; &nbsp; &nbsp; <a onclick="onDelete()" style="color: #007bff; cursor: pointer"> delete</a>&nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; </tr>&nbsp; </tbody></table>

慕标5832272

thisonclick指的是该方法所属的对象。我用 更改了您的 HTML 代码onclick="onUpdate(this)",现在您可以使用函数影响该行的文本map()。此外,您还需要更改选择器以选择<td>单击的内容<tr>,因此我使用了[parent().parent()][1]方法来实现这一点。您可以使用替代选择器,例如[closest()][1]var tableData;function onUpdate(e) {&nbsp; tableData = $(e).parent().parent().children("td").map(function () {&nbsp; &nbsp; &nbsp; &nbsp; return $(this).text();&nbsp; &nbsp; }).get();&nbsp; &nbsp;&nbsp;&nbsp; console.log("Table data has created as; "+tableData);&nbsp; if( tableData[7] === 2) {&nbsp; &nbsp; console.log("if statement");&nbsp; }}<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><table class="table table-bordered table-responsive-sm table-hover">&nbsp; &nbsp; <thead>&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Date</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>TimeSlot</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Room</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Attendees</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>update</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>delete</th>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; </thead>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<tbody>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr class="table">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td class="table">...</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td class="table">...</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td class="table">...</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td class="table">...</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td class="table" style="display: none">...</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td class="table" style="display: none">...</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td class="table" style="display: none">...</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td class="table" style="display: none">...</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td class="table command">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a onclick="onUpdate(this)" style="color: #007bff; cursor: pointer"> update</a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td class="table command">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a onclick="onDelete(this)" style="color: #007bff; cursor: pointer"> delete</a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; </tbody></table>

慕仙森

将引用传递this给内联函数:<a onclick="onUpdate(this)" />然后在处理程序中使用锚引用 ieref来获取行:function onUpdate(ref) {&nbsp; const $row = $(ref).closest('tr');}完整示例function onUpdate(ref) {&nbsp; const $row = $(ref).closest('tr'),&nbsp; &nbsp; rowData = $row.find('td:not(.actions)').map(function(td) {&nbsp; &nbsp; &nbsp; return $(this).text();&nbsp; &nbsp; }).get();&nbsp; console.log(`Row data: ${JSON.stringify(rowData)}`);&nbsp; if (rowData[0] === '2020-12-23') {&nbsp; &nbsp; console.log('Hello World!');&nbsp; }}function onDelete(ref) {&nbsp; $(ref).closest('tr').remove();}thead tr th {&nbsp; text-align: center;}.actions {&nbsp; display: grid;&nbsp; grid-auto-flow: column;&nbsp; column-gap: 1em;&nbsp; justify-content: center;&nbsp; align-content: space-evenly;}.hidden {&nbsp; display: none;}<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css" rel="stylesheet"/><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"><link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous"><script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script><table class="table table-bordered table-responsive-sm table-hover">&nbsp; <thead>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <th>Date</th>&nbsp; &nbsp; &nbsp; <th>Time Slot</th>&nbsp; &nbsp; &nbsp; <th>Room</th>&nbsp; &nbsp; &nbsp; <th>Attendees</th>&nbsp; &nbsp; &nbsp; <th class="hidden">Hidden #1</th>&nbsp; &nbsp; &nbsp; <th class="hidden">Hidden #2</th>&nbsp; &nbsp; &nbsp; <th class="hidden">Hidden #3</th>&nbsp; &nbsp; &nbsp; <th>Actions</th>&nbsp; &nbsp; </tr>&nbsp; </thead>&nbsp; <tbody>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <td>2020-12-23</td>&nbsp; &nbsp; &nbsp; <td>12:00</td>&nbsp; &nbsp; &nbsp; <td>42</td>&nbsp; &nbsp; &nbsp; <td>8</td>&nbsp; &nbsp; &nbsp; <td class="hidden">...</td>&nbsp; &nbsp; &nbsp; <td class="hidden">...</td>&nbsp; &nbsp; &nbsp; <td class="hidden">...</td>&nbsp; &nbsp; &nbsp; <td class="actions">&nbsp; &nbsp; &nbsp; &nbsp; <a onclick="onUpdate(this)" style="color: #007bff; cursor: pointer">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <i class="fas fa-pencil-alt" aria-hidden="true"></i>&nbsp; &nbsp; &nbsp; &nbsp; </a>&nbsp; &nbsp; &nbsp; &nbsp; <a onclick="onDelete(this)" style="color: #007bff; cursor: pointer">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <i class="fas fa-trash" aria-hidden="true"></i>&nbsp; &nbsp; &nbsp; &nbsp; </a>&nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; </tr>&nbsp; </tbody></table>

胡说叔叔

您不需要该单击功能,因此您可以通过这种方式修改您的代码。<script>&nbsp; &nbsp; function onUpdate() {&nbsp; &nbsp; &nbsp; var tableData = $("tr.table")&nbsp; &nbsp; &nbsp; &nbsp; .children("td")&nbsp; &nbsp; &nbsp; &nbsp; .map(function () {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $(this).text();&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; .get();&nbsp; &nbsp; &nbsp; if( tableData[7] === 2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;console.log("if statement");&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; </script>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript