【问题标题】:Uncaught ReferenceError: myFunction is not defined at HTMLButtonElement.onclick in JQuery and HTML未捕获的 ReferenceError:myFunction 未在 JQuery 和 HTML 中的 HTMLButtonElement.onclick 中定义
【发布时间】:2020-09-08 16:52:53
【问题描述】:

我正在应用 JQuery:

HTML

    <div id="myNotice" class="container-fluid">

            <div style="margin-bottom: 10px;" class="row">
                <div class="col-lg-12" >
                    <button type="button" onclick="myFunction()" class="btn btn-block btn-info">
                        <i class="fas fa-arrow-right"></i> 
                            <span >Proceed</span>
                    </button>
                </div>
            </div>   
    </div>


  <div id="myMSF" class="container-fluid" style="display:none">
                 <table id="msfTable" class=" table table-bordered table-striped table-hover datatable">
                        <thead>
                            <tr>
                                <th scope="col" width="4%">ID</th>
                                <th scope="col" width="25%">Core Value<span style="color:red;">*</span></th>                                    
                                <th scope="col" width="25%">Rating<span style="color:red;">*</span></th>                                    
                                <th scope="col" width="46%">Comment</th>
                            </tr>
                        </thead> 
                        <tbody class="resultbody">

                        </tbody>
                    </table>  
  </div>

jQuery

<script type="text/javascript">
  $(document).ready(function() {  
         //Try to get tbody first with jquery children. works faster!
     var tbody = $('#msfTable').children('tbody');
     
     //Then if no tbody just select your table 
     var table = tbody.length ? tbody : $('#msfTable');
     $('[name=count_skills_no]').text();

  function myFunction() {
    var x = document.getElementById("myMSF");
    var y = document.getElementById("myNotice");
    if (x.style.display === "none") {
      x.style.display = "block";
      y.style.display = "none";
    } else {
      x.style.display = "none";
      y.style.display = "block";
    }
     var nx = ($('.resultbody tr').length - 0) + 1;
     var num_rows = $("#msfTable tbody tr").length + 1; 
    var rows = $('[name=count_skills_no]').val() - 1;
    var sn;
    for (var i = 0; i < rows; i++) {
        sn = num_rows + i;
     //Add row
     table.append('<tr>\n' +
                        '<td class="no">' + sn + '</td>\n' +
                        '        <td><select class="form-control select2bs4" data-placeholder="Choose Employee Type" tabindex="1" name="appraisal_skill_id[]" required>\n' +
                        '             <option value="0" selected="true" disabled="true">Select Core Value</option>\n' +
                        '              @if($skills->count() > 0 )\n' +
                        '                 @foreach($skills as $skill)\n' +
                        '                 <option value="{{$skill->id}}">{{$skill->skill_name}}</option>\n' +
                        '                 @endforeach\n' +
                        '             @endif\n' +
                        '         </select></td>\n' +
                        '        <td><select class="form-control select2bs4" data-placeholder="Choose Rating" tabindex="1" name="appraisal_respondent_rating_id[]" required>\n' +
                        '             <option value="0" selected="true" disabled="true">Select Rating</option>\n' +
                        '              @if($ratings->count() > 0 )\n' +
                        '                 @foreach($ratings as $rating)\n' +
                        '                 <option value="{{$rating->id}}">{{$rating->rating_value}} - {{$rating->rating_name}}</option>\n' +
                        '                 @endforeach\n' +
                        '             @endif\n' +
                        '         </select></td>\n' +
                        '   <td><input type="text" name="comment[]" placeholder="Enter comment here" class="form-control comment" required></td>\n' +
    '</tr>');
     }
  }
  });
 </script>

默认

<div id="myMSF" class="container-fluid" style="display:none"> 

隐藏时

<div id="myNotice" class="container-fluid"> 

可见。

                    <button type="button" onclick="myFunction()" class="btn btn-block btn-info">
                        <i class="fas fa-arrow-right"></i> 
                            <span >Proceed</span>
                    </button>

是显示 myMSF 并隐藏。它还创建了表格的主体

当按钮继续是 onclick 时,我收到此错误:

未捕获的 ReferenceError:myFunction 未在 HTMLButtonElement.onclick 中定义

这段代码被高亮显示

 <button type="button" onclick="myFunction()" class="btn btn-block btn-info">

我该如何解决?

谢谢

【问题讨论】:

    标签: html jquery


    【解决方案1】:

    您在 $(document).ready(function() { 的上下文中定义了 function myFunction() {。因此,它在外部不可见。移动此类dom 外的函数已准备就绪。

    此外,避免内联事件处理程序,改用.on()

    $('#myNotice button.btn-info').on('click', function(e) {
        .....
    })
    

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
    
    <div id="myNotice" class="container-fluid">
    
        <div style="margin-bottom: 10px;" class="row">
            <div class="col-lg-12" >
                <button type="button" onclick="myFunction()" class="btn btn-block btn-info">
                    <i class="fas fa-arrow-right"></i>
                    <span >Proceed</span>
                </button>
            </div>
        </div>
    </div>
    
    
    <div id="myMSF" class="container-fluid" style="display:none">
        <table id="msfTable" class=" table table-bordered table-striped table-hover datatable">
            <thead>
            <tr>
                <th scope="col" width="4%">ID</th>
                <th scope="col" width="25%">Core Value<span style="color:red;">*</span></th>
                <th scope="col" width="25%">Rating<span style="color:red;">*</span></th>
                <th scope="col" width="46%">Comment</th>
            </tr>
            </thead>
            <tbody class="resultbody">
    
            </tbody>
        </table>
    </div>
    
    <script>
    function myFunction() {
        var x = document.getElementById("myMSF");
        var y = document.getElementById("myNotice");
        if (x.style.display === "none") {
            x.style.display = "block";
            y.style.display = "none";
        } else {
            x.style.display = "none";
            y.style.display = "block";
        }
        var nx = ($('.resultbody tr').length - 0) + 1;
        var num_rows = $("#msfTable tbody tr").length + 1;
        var rows = $('[name=count_skills_no]').val() - 1;
        var sn;
        for (var i = 0; i < rows; i++) {
            sn = num_rows + i;
            //Add row
            table.append('<tr>\n' +
                    '<td class="no">' + sn + '</td>\n' +
                    '        <td><select class="form-control select2bs4" data-placeholder="Choose Employee Type" tabindex="1" name="appraisal_skill_id[]" required>\n' +
                    '             <option value="0" selected="true" disabled="true">Select Core Value</option>\n' +
                    '              @if($skills->count() > 0 )\n' +
                    '                 @foreach($skills as $skill)\n' +
                    '                 <option value="{{$skill->id}}">{{$skill->skill_name}}</option>\n' +
                    '                 @endforeach\n' +
                    '             @endif\n' +
                    '         </select></td>\n' +
                    '        <td><select class="form-control select2bs4" data-placeholder="Choose Rating" tabindex="1" name="appraisal_respondent_rating_id[]" required>\n' +
                    '             <option value="0" selected="true" disabled="true">Select Rating</option>\n' +
                    '              @if($ratings->count() > 0 )\n' +
                    '                 @foreach($ratings as $rating)\n' +
                    '                 <option value="{{$rating->id}}">{{$rating->rating_value}} - {{$rating->rating_name}}</option>\n' +
                    '                 @endforeach\n' +
                    '             @endif\n' +
                    '         </select></td>\n' +
                    '   <td><input type="text" name="comment[]" placeholder="Enter comment here" class="form-control comment" required></td>\n' +
                    '</tr>');
        }
    }
    $(document).ready(function() {
        //Try to get tbody first with jquery children. works faster!
        var tbody = $('#msfTable').children('tbody');
    
        //Then if no tbody just select your table
        var table = tbody.length ? tbody : $('#msfTable');
        $('[name=count_skills_no]').text();
    });
    </script>

    【讨论】:

    • @gaeranoM - 我有这个新错误:Uncaught ReferenceError: table is not defined at myFunction (13:759) at HTMLButtonElement.onclick (并且这一行突出显示:table.append(' \n' +
    • - 我复制了相同的代码并使用了。正如我之前所说,它有这个错误: Uncaught ReferenceError: table is not defined at myFunction (13:759) at HTMLButtonElement.onclick (并且这一行被突出显示:table.append('\n' + 我认为可能没有看到: var table = tbody.length ? tbody : $('#msfTable');
    • @mikefolu 代码段已更新。请创建一个空的 html 页面并将 mu sn-p 复制并粘贴到正文部分。告诉我
    • -好的。让我这样做
    • @mikefolu 你的 sn-p 有很多乱七八糟的东西。我删除了内联按钮点击。我删除了按钮类型提交(这应该通过 aiax 调用完成)。我改变了你用来切换可见性的方式。但是还有很多其他需要添加/修复。我希望你不能假设我为你写代码。这不是编码服务。更新的小提琴here。我希望你能够明白。非常感谢
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签