将变量传递给 jquery 模态弹出窗口

Pass variable to jquery modal popup

提问人:Jayreis 提问时间:12/15/2022 最后编辑:Jayreis 更新时间:12/16/2022 访问量:198

问:

我有一个从 mysql 数据库中提取的 url 列表,需要将所选的特定 url 传递给 jQuery 模式窗口。 我的问题是我无法弄清楚如何传递数据并将其显示在jQuery模式中。下面是我的代码div

   <table class="table responsive">
       <thead><th>Topic</th><th></th></thead>
       <tbody>
           <tr>
                      <td>Phone System</td>
                      <td>
                        <button type="button" class="btn btn-default vidurl" data-videourl="https://foobar.s3.amazonaws.com/fake/url/vid.webm" data-toggle="modal" data-target="#modal-default">View Video</button>
                      </td>
                    </tr>
                    <tr>
                      <td>Ticket System</td>
                      <td>
                        <button type="button" class="btn btn-default vidurl" data-videourl="https://foobar.s3.amazonaws.com/fake/url/ticketvidinfo.webm" data-toggle="modal" data-target="#modal-default">View Video</button>
                      </td>
         </tbody>
    </table>

    <div class="modal fade" id="modal-default">
        <div class="modal-dialog">
          <div class="modal-content">
            <div class="modal-header">
              <h4 class="modal-title">Training Video</h4>
              <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
              </button>
            </div>
            <div class="modal-body">
              <span class="viddata">what to show the video url here </span>
            </div>
            <div class="modal-footer justify-content-between">
              <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
          </div>
          <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
     </div>
     <!-- /.modal -->
     <!-- below is my attempt to get the data from the button that was clicked and then set it as a variable and then make it load in a span inside the modal -->
     <script>
         $(document).ready(function(){
            $(".vidurl").click(function(){
               var vidurl = $('.vidurl').data(videourl);
               $('.viddata').text(videourl);
               console.log(videourl);
            });
         });
     </script>
jquery 参数传递

评论

0赞 Jayreis 12/15/2022
感谢@cytek04的编辑。我仍在寻找如何解决我的问题
0赞 Jayreis 12/16/2022
我现在收到一个错误,说 Uncaught ReferenceError: videourl is not defined I have edited my original message with update code
0赞 cytek04 12/16/2022
我的错,你需要引号。查看更新的答案

答:

0赞 cytek04 12/15/2022 #1

您没有完全正确地使用该属性。 是自定义属性。像 .然后,您可以在jQuery中访问,例如.dataDatadata-mycustomname$(selector).data("mycustomname")

<button type="button" class="btn btn-default vidurl" data-url="https://foobar.s3.amazonaws.com/fake/url/vid.webm" data-toggle="modal" data-target="#modal-default">View Video</button>

然后在你的jQuery中

var vidurl = $('.vidurl').data("url");

HTML data-* 属性

jQuery data() 方法