打开 Bootstrap 模态框时出错的 JavaScript

huangapple go评论63阅读模式
英文:

Error Opening Bootstrap Modal With JavaScript

问题

I understand that you want a translation for the code-related portions of your text. Here are the code sections translated into Chinese:

我正在开发一个涉及数据库行列表的用户界面部分每行都是可编辑的当点击**编辑**按钮时我的意图是弹出一个模态对话框窗口通过 AJAX 预加载数据以在表单中编辑特定行如下图所示

以下的代码展示了一个可供您测试的工作设置

```javascript
jQuery(document).ready(function() {
  // console.log('已加载播客剧集列表的JavaScript');
  // jQuery('#editModal').modal('show');
});

function loadEditModal(rowID) {
  console.log("点击了行 " + rowID + "\n");
  // 填充模态窗口的内容
  // jQuery('#editModal').modal('show');
  modal1 = bootstrap.Modal.getOrCreateInstance('#editModal');
  modal1.show();
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
<div class="container">
  <!-- 显示编码队列内容的表格 -->
  <div class="row">
    <div class="table-responsive-xxl">
      <table class="table table-hover table-sm">
        <thead>
          <tr>
            <th scope="col">ID#</th>
            <!-- episode_id -->
            <th scope="col">播客</th>
            <!-- podcast_code -->
            <th scope="col">标题</th>
            <th scope="col">Seq.</th>
            <th scope="col">状态</th>
            <!-- last_action -->
            <th scope="col">日期</th>
            <!-- last_action_date -->
            <th scope="col"></th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>8</td>
            <td>bw</td>
            <td>建造鸟屋</td>
            <td>345</td>
            <td>{"error":"none","message":"初始插入","status":"处理中"}</td>
            <td>2023-03-22</td>
            <td class="text-end" style="white-space: nowrap">
              <!-- <button disabled type="button" class="btn btn-outline-secondary btn-sm">编辑</button> -->
              <button type="button" class="btn btn-outline-secondary btn-sm" data-bs-toggle="modal" onclick="loadEditModal(8)">编辑</button>
              <!-- <button disabled type="button" class="btn btn-outline-secondary btn-sm">重新发布</button> -->
              <button type="button" class="btn btn-outline-secondary btn-sm">重新发布</button>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
  <!-- 行的编辑模态框 -->
  <div class="row">
    <div class="modal fade" id="editModal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="editModalLabel" style="display: none;" aria-hidden="true">
      <div class="modal-dialog modal-dialog-centered modal-lg">
        <div class="modal-content">
          <div class="modal-header">
            <h1 class="modal-title fs-5" id="editModalLabel">模态框标题</h1>
            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="关闭"></button>
          </div>
          <div class="modal-body" id="editModalBody">
            这里是模态框的内容...
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
            <button type="button" class="btn btn-primary">保存更改</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

然而,每当单击编辑按钮以显示模态框时,我在 JavaScript 控制台中会收到以下错误:

未捕获的 TypeError: 无法访问属性 "backdrop",this._config 未定义

这似乎是因为我通过 JavaScript 使用 onclick 事件启动模态框。如果我稍微更改 HTML 和 JavaScript 如下,并使用按钮的"data-bs-target"属性来打开模态框,就可以正常工作,没有 JavaScript 错误。

jQuery(document).ready(function() {
  // console.log('已加载播客剧集列表的JavaScript');
  // jQuery('#editModal').modal('show');
});

function loadEditModal(rowID) {
  console.log("点击了行 " + rowID + "\n");
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
  <!-- 显示编码队列内容的表格 -->
  <div class="row">
    <div class="table-responsive-xxl">
      <table class="table table-hover table-sm">
        <thead>
          <tr>
            <th scope="col">ID#</th>
            <!-- episode_id -->
            <th scope="col">播客</th>
            <!-- podcast_code -->
            <th scope="col">标题</th>
            <th scope="col">Seq.</th>
            <th scope="col">状态</th>
            <!-- last_action -->
            <th scope="col">日期</th>
            <!-- last_action_date -->
            <th scope="col"></th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>8</td>
            <td>bw</td>
            <td>建造鸟屋</td>
            <td>345</td>
            <td>{"error":"none","message":"初始插入","status":"处理中"}</td>
            <td>2023-03-22</td>
            <td class="text-end" style="white-space: nowrap">
              <!-- <button disabled type="button" class="btn btn-outline-secondary btn-sm">编辑</button> -->
              <button type="button" class="btn btn-outline-secondary btn-sm" data-bs-toggle="modal" data-bs-target="#editModal" onclick="loadEditModal(8)">编辑</button

<details>
<summary>英文:</summary>

I&#39;m working on a piece of UI that involves a listing of rows in the database. Each row is editable, and when the **edit** button is clicked, my intent is to bring up a modal dialogue window with data preloaded in a form via AJAX for editing that specific row as shown in the following picture. 

[![image of table row][1]][1]

The following code shows a working setup you can play with. 

&lt;!-- begin snippet: js hide: true console: true babel: false --&gt;

&lt;!-- language: lang-js --&gt;

    jQuery(document).ready(function() {
      // console.log(&#39;podcast episode list js loaded&#39;);
      // jQuery(&#39;#editModal&#39;).modal(&#39;show&#39;);
    });

    function loadEditModal(rowID) {
      console.log(&quot;Clicked row &quot; + rowID + &quot;\n&quot;);
      //populate the content of the modal window
      //jQuery(&#39;#editModal&#39;).modal(&#39;show&#39;);
      modal1 = bootstrap.Modal.getOrCreateInstance(&#39;#editModal&#39;);
      modal1.show();
    }

&lt;!-- language: lang-html --&gt;

    &lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; /&gt;
    &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.3/jquery.min.js&quot;&gt;&lt;/script&gt;
    &lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js&quot;&gt;&lt;/script&gt;
    &lt;div class=&quot;container&quot;&gt;
      &lt;!-- table displaying contents of encoding queue --&gt;
      &lt;div class=&quot;row&quot;&gt;
        &lt;div class=&quot;table-responsive-xxl&quot;&gt;
          &lt;table class=&quot;table table-hover table-sm&quot;&gt;
            &lt;thead&gt;
              &lt;tr&gt;
                &lt;th scope=&quot;col&quot;&gt;ID#&lt;/th&gt;
                &lt;!-- episode_id --&gt;
                &lt;th scope=&quot;col&quot;&gt;Podcast&lt;/th&gt;
                &lt;!-- podcast_code --&gt;
                &lt;th scope=&quot;col&quot;&gt;Title&lt;/th&gt;
                &lt;th scope=&quot;col&quot;&gt;Seq.&lt;/th&gt;
                &lt;th scope=&quot;col&quot;&gt;Status&lt;/th&gt;
                &lt;!-- last_action --&gt;
                &lt;th scope=&quot;col&quot;&gt;Date&lt;/th&gt;
                &lt;!-- last_action_date --&gt;
                &lt;th scope=&quot;col&quot;&gt;&lt;/th&gt;
              &lt;/tr&gt;
            &lt;/thead&gt;
            &lt;tbody&gt;
              &lt;tr&gt;
                &lt;td&gt;8&lt;/td&gt;
                &lt;td&gt;bw&lt;/td&gt;
                &lt;td&gt;Build a birdhouse&lt;/td&gt;
                &lt;td&gt;345&lt;/td&gt;
                &lt;td&gt;{&quot;error&quot;:&quot;none&quot;,&quot;message&quot;:&quot;initial insert&quot;,&quot;status&quot;:&quot;processing&quot;}&lt;/td&gt;
                &lt;td&gt;2023-03-22&lt;/td&gt;
                &lt;td class=&quot;text-end&quot; style=&quot;white-space: nowrap&quot;&gt;
                  &lt;!-- &lt;button disabled type=&quot;button&quot; class=&quot;btn btn-outline-secondary btn-sm&quot;&gt;Edit&lt;/button&gt; --&gt;
                  &lt;button type=&quot;button&quot; class=&quot;btn btn-outline-secondary btn-sm&quot; data-bs-toggle=&quot;modal&quot; onclick=&quot;loadEditModal(8)&quot;&gt;Edit&lt;/button&gt;
                  &lt;!-- &lt;button disabled type=&quot;button&quot; class=&quot;btn btn-outline-secondary btn-sm&quot;&gt;Republish&lt;/button&gt; --&gt;
                  &lt;button type=&quot;button&quot; class=&quot;btn btn-outline-secondary btn-sm&quot;&gt;Republish&lt;/button&gt;
                &lt;/td&gt;
              &lt;/tr&gt;

            &lt;/tbody&gt;
          &lt;/table&gt;
        &lt;/div&gt;
      &lt;/div&gt;
      &lt;!-- class=&quot;row&quot; --&gt;

      &lt;!-- modal for editing rows --&gt;
      &lt;div class=&quot;row&quot;&gt;
        &lt;div class=&quot;modal fade&quot; id=&quot;editModal&quot; data-bs-backdrop=&quot;static&quot; data-bs-keyboard=&quot;false&quot; tabindex=&quot;-1&quot; aria-labelledby=&quot;editModalLabel&quot; style=&quot;display: none;&quot; aria-hidden=&quot;true&quot;&gt;
          &lt;div class=&quot;modal-dialog modal-dialog-centered modal-lg&quot;&gt;
            &lt;div class=&quot;modal-content&quot;&gt;
              &lt;div class=&quot;modal-header&quot;&gt;
                &lt;h1 class=&quot;modal-title fs-5&quot; id=&quot;editModalLabel&quot;&gt;Modal title&lt;/h1&gt;
                &lt;button type=&quot;button&quot; class=&quot;btn-close&quot; data-bs-dismiss=&quot;modal&quot; aria-label=&quot;Close&quot;&gt;&lt;/button&gt;
              &lt;/div&gt;
              &lt;div class=&quot;modal-body&quot; id=&quot;editModalBody&quot;&gt;
                this is where the body content goes...
              &lt;/div&gt;
              &lt;div class=&quot;modal-footer&quot;&gt;
                &lt;button type=&quot;button&quot; class=&quot;btn btn-secondary&quot; data-bs-dismiss=&quot;modal&quot;&gt;Close&lt;/button&gt;
                &lt;button type=&quot;button&quot; class=&quot;btn btn-primary&quot;&gt;Save changes&lt;/button&gt;
              &lt;/div&gt;
            &lt;/div&gt;
          &lt;/div&gt;
        &lt;/div&gt;

      &lt;/div&gt;
    &lt;/div&gt;

&lt;!-- end snippet --&gt;

However, I&#39;m getting the following error in the js console every time the **edit** button is clicked to display the modal:

&gt; Uncaught TypeError: can&#39;t access property &quot;backdrop&quot;, this._config is undefined

[![javascript error][2]][2]

This appears to be due to the fact that I&#39;m launching the modal via javascript with the onclick event. If I change the html and js slightly as follows, and use the &quot;data-bs-target&quot; property of the button to open the modal instead, it all works without js errors.

&lt;!-- begin snippet: js hide: true console: true babel: false --&gt;

&lt;!-- language: lang-js --&gt;

    jQuery(document).ready(function() {
      // console.log(&#39;podcast episode list js loaded&#39;);
      // jQuery(&#39;#editModal&#39;).modal(&#39;show&#39;);
    });

    function loadEditModal(rowID) {
      console.log(&quot;Clicked row &quot; + rowID + &quot;\n&quot;);
    }

&lt;!-- language: lang-html --&gt;

    &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.3/jquery.min.js&quot;&gt;&lt;/script&gt;
    &lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js&quot;&gt;&lt;/script&gt;
    &lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; /&gt;
    &lt;div class=&quot;container&quot;&gt;
      &lt;!-- table displaying contents of encoding queue --&gt;
      &lt;div class=&quot;row&quot;&gt;
        &lt;div class=&quot;table-responsive-xxl&quot;&gt;
          &lt;table class=&quot;table table-hover table-sm&quot;&gt;
            &lt;thead&gt;
              &lt;tr&gt;
                &lt;th scope=&quot;col&quot;&gt;ID#&lt;/th&gt;
                &lt;!-- episode_id --&gt;
                &lt;th scope=&quot;col&quot;&gt;Podcast&lt;/th&gt;
                &lt;!-- podcast_code --&gt;
                &lt;th scope=&quot;col&quot;&gt;Title&lt;/th&gt;
                &lt;th scope=&quot;col&quot;&gt;Seq.&lt;/th&gt;
                &lt;th scope=&quot;col&quot;&gt;Status&lt;/th&gt;
                &lt;!-- last_action --&gt;
                &lt;th scope=&quot;col&quot;&gt;Date&lt;/th&gt;
                &lt;!-- last_action_date --&gt;
                &lt;th scope=&quot;col&quot;&gt;&lt;/th&gt;
              &lt;/tr&gt;
            &lt;/thead&gt;
            &lt;tbody&gt;
              &lt;tr&gt;
                &lt;td&gt;8&lt;/td&gt;
                &lt;td&gt;bw&lt;/td&gt;
                &lt;td&gt;Build a birdhouse&lt;/td&gt;
                &lt;td&gt;345&lt;/td&gt;
                &lt;td&gt;{&quot;error&quot;:&quot;none&quot;,&quot;message&quot;:&quot;initial insert&quot;,&quot;status&quot;:&quot;processing&quot;}&lt;/td&gt;
                &lt;td&gt;2023-03-22&lt;/td&gt;
                &lt;td class=&quot;text-end&quot; style=&quot;white-space: nowrap&quot;&gt;
                  &lt;!-- &lt;button disabled type=&quot;button&quot; class=&quot;btn btn-outline-secondary btn-sm&quot;&gt;Edit&lt;/button&gt; --&gt;
                  &lt;button type=&quot;button&quot; class=&quot;btn btn-outline-secondary btn-sm&quot; data-bs-toggle=&quot;modal&quot; data-bs-target=&quot;#editModal&quot; onclick=&quot;loadEditModal(8)&quot;&gt;Edit&lt;/button&gt;
                  &lt;!-- &lt;button disabled type=&quot;button&quot; class=&quot;btn btn-outline-secondary btn-sm&quot;&gt;Republish&lt;/button&gt; --&gt;
                  &lt;button type=&quot;button&quot; class=&quot;btn btn-outline-secondary btn-sm&quot;&gt;Republish&lt;/button&gt;
                &lt;/td&gt;
              &lt;/tr&gt;

            &lt;/tbody&gt;
          &lt;/table&gt;
        &lt;/div&gt;
      &lt;/div&gt;
      &lt;!-- class=&quot;row&quot; --&gt;

      &lt;!-- modal for editing rows --&gt;
      &lt;div class=&quot;row&quot;&gt;
        &lt;div class=&quot;modal fade&quot; id=&quot;editModal&quot; data-bs-backdrop=&quot;static&quot; data-bs-keyboard=&quot;false&quot; tabindex=&quot;-1&quot; aria-labelledby=&quot;editModalLabel&quot; style=&quot;display: none;&quot; aria-hidden=&quot;true&quot;&gt;
          &lt;div class=&quot;modal-dialog modal-dialog-centered modal-lg&quot;&gt;
            &lt;div class=&quot;modal-content&quot;&gt;
              &lt;div class=&quot;modal-header&quot;&gt;
                &lt;h1 class=&quot;modal-title fs-5&quot; id=&quot;editModalLabel&quot;&gt;Modal title&lt;/h1&gt;
                &lt;button type=&quot;button&quot; class=&quot;btn-close&quot; data-bs-dismiss=&quot;modal&quot; aria-label=&quot;Close&quot;&gt;&lt;/button&gt;
              &lt;/div&gt;
              &lt;div class=&quot;modal-body&quot; id=&quot;editModalBody&quot;&gt;
                this is where the body content goes...
              &lt;/div&gt;
              &lt;div class=&quot;modal-footer&quot;&gt;
                &lt;button type=&quot;button&quot; class=&quot;btn btn-secondary&quot; data-bs-dismiss=&quot;modal&quot;&gt;Close&lt;/button&gt;
                &lt;button type=&quot;button&quot; class=&quot;btn btn-primary&quot;&gt;Save changes&lt;/button&gt;
              &lt;/div&gt;
            &lt;/div&gt;
          &lt;/div&gt;
        &lt;/div&gt;

      &lt;/div&gt;
    &lt;/div&gt;

&lt;!-- end snippet --&gt;

But I can&#39;t do it this way because I have to fetch the data to edit with AJAX and populate the body of the modal before I display it. Can anyone clue me in to what I need to do to eliminate the error?


  [1]: https://i.stack.imgur.com/q9y8f.png
  [2]: https://i.stack.imgur.com/Zyu8X.png

</details>


# 答案1
**得分**: 2

This appears to be due to the fact that I'm launching the modal via javascript with the onclick event.

This is not correct.

The problem occurs because you are using the `data-bs-toggle="modal"` attribute without specifying what the target is.

If you try to remove the onclick attribute from the button the problem still exists.

<button type="button" class="btn btn-outline-secondary btn-sm" data-bs-toggle="modal">Edit</button>

Just remove the `data-bs-toggle="modal"` attribute from the edit button and use onclick only.

<button type="button" class="btn btn-outline-secondary btn-sm" onclick="loadEditModal(8)">Edit</button>

I hope it helps.

<details>
<summary>英文:</summary>

&gt; This appears to be due to the fact that I&#39;m launching the modal via javascript with the onclick event.

This is not correct. 

The problem occurs because you are using the `data-bs-toggle=&quot;modal&quot;` attribute without specifying what the target is.


If you try to remove the onclick attribute from the button the problem still exist.

    &lt;button type=&quot;button&quot; class=&quot;btn btn-outline-secondary btn-sm&quot; data-bs-toggle=&quot;modal&quot;&gt;Edit&lt;/button&gt;


Just remove the `data-bs-toggle=&quot;modal&quot;` attribute from the edit button and use onclick only.

    &lt;button type=&quot;button&quot; class=&quot;btn btn-outline-secondary btn-sm&quot; onclick=&quot;loadEditModal(8)&quot;&gt;Edit&lt;/button&gt;

I hope it helps

</details>



huangapple
  • 本文由 发表于 2023年2月10日 03:44:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75403695.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定