twitter-bootstrap 如何获取 data-id 的值并将其放入 Twitter 引导模式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25861452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 21:33:15  来源:igfitidea点击:

How to get value of data-id and place it into a twitter bootstrap modal?

javascriptphpjquerytwitter-bootstrap

提问by Nomad

Im trying to get the value of the data-id which contains and id in my database. I need to get the admin_id and place it into a twitter bootstrap modal. How am I going to get that value using java script?

我试图获取我的数据库中包含和 id 的数据 ID 的值。我需要获取 admin_id 并将其放入 Twitter 引导模式。我将如何使用 java 脚本获得该值?

<a data-toggle="modal" data-id="<?=$row['ADMIN_ID'];?>" href="#view_contact" class="btn btn-info btn-xs view-admin">View</a>

here is my modal

这是我的模态

            <div class="modal fade" id="view_contact" role="dialog">
                <div class="modal-dialog">
                    <div class="modal-content">

                        <div class="modal-header">
                            <h4>Admin modal!!</h4>

                        </div>

                        <div class="modal-body">
                            Admin Id:<p id="showid"></p>
                        </div>

                        <div class="modal-footer">

                            <a class="btn btn-default" data-dismiss="modal">Close</a>

                        </div>


                    </div>

                </div>

            </div>

here is my java script

这是我的 Java 脚本

            <script type="text/javascript">


            $(document).on("click", ".view-admin", function () {
                 var adminid = $(this).data('id');
                 $(".modal-body #showid").val( adminid );
                 $('#view_contact').modal('show');
            });

            </script>

its not showing in my modal. They are all in the same page. How do I do this? Kinda new at this.

它没有显示在我的模式中。它们都在同一页面上。我该怎么做呢?这方面有点新。

采纳答案by Anoop Joshi

Problem is, you are trying to assign the value to a ptag. ptag does not have value property. Use text()or html()

问题是,您正在尝试将值分配给p标签。p标签没有 value 属性。使用text()html()

$(document).on("click", ".view-admin", function() {
    var adminid = $(this).data('id');
    $(".modal-body #showid").text(adminid);
    $('#view_contact').modal('show');
});

回答by KCP

You can just replace

你可以更换

$(".modal-body #showid").val( adminid );

with

$("#showid").text( adminid );

You don't need to reference class name .modal-bodyfor the id #showidas there must be only one id name in a page according to W3C Validation. And referencing with the id is the fastest way to access any html elements.

您不需要.modal-body为 id引用类名,#showid因为根据W3C 验证,页面中必须只有一个 id 名称。使用 id 引用是访问任何 html 元素的最快方法。