Javascript 如何从函数调用 Jquery 打开模态

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36621481/
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-08-23 19:18:26  来源:igfitidea点击:

How to Open Modal from Function Call Jquery

javascriptjqueryhtmltwitter-bootstrapbootstrap-modal

提问by sara adly

I have this Modal from Bootstrap When I add button To call it it open successfully, but what I really need is to open this modal from function calling automatically my Experiment is:

当我添加按钮时,我从 Bootstrap 获得了这个 Modal 调用它成功打开,但我真正需要的是从函数调用自动打开这个模态我的实验是:

     <button id="RenewCollerctorDateID" class="btn btn-success" style="width: 10%; display: inline;
 padding-right: 10px; float: left;" onclick="RenewCollectorDatePeriod();">renew</button>

MY JavaScript is

我的 JavaScript 是

 function RenewCollectorDatePeriod() {
         //   AreYOuSureRenew();        
            var EmpID = $("#<%=ddlFilterCollector.ClientID%>").val();
            if (EmpID == -1) {
                alert("please select one ")
            }
            else {
               alert(EmpID);
              GetCollectorInfo(EmpID);       
            }        
        }

then:

然后:

        function GetCollectorInfo(EmpID) {
       //     AreYOuSureRenew();
            $.ajax({
                type: "POST",
                url: "UnloadingCalendar.aspx/GetCollectorInfo",
                data: JSON.stringify({ EmpID: EmpID }),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (result) {
                    alert(result.d);
                    AreYOuSureRenew();
                    },
                error: function (msg) {
                    alert(msg.d);
                },
                complete: function () {

                }
            })
        }


    function AreYOuSureRenew() {
        alert("opened");
        $('#EnsureModal').modal('show');
    }

and here my modal

这里是我的模态

    <div class=" modal fade" id="EnSureModal" role="dialog">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title">Do ypu need change </h4>
                </div>
                <div class="modal-body">
                    <p>Are u sure from </p>
                    <label id="FromDate"></label>
                    <p>To</p>
                    <label id="ToDate"></label>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">no</button>
                    <button type="button" class="btn btn-default" data-dismiss="modal">yes</button>
                </div>
            </div>
        </div>
    </div>

Notice: when I add $("#EnSureModal").modal('show');in Document.Ready it appeasrs on page load and appears again on function call , how can I make it appears only in function call

注意:当我添加$("#EnSureModal").modal('show');Document.Ready 时,它会在页面加载时出现并再次出现在函数调用中,如何让它只出现在函数调用中

回答by claudios

I read your code and I found out that you didn't use bootstrap correctly and the also the script you wrote is not correct. Check this out. This might help you.

我阅读了您的代码,发现您没有正确使用引导程序,而且您编写的脚本也不正确。看一下这个。这可能对你有帮助。

Script:

脚本:

$(document).ready(function(){
    $("#fireme").click(function(){
        $("#EnSureModal").modal();
    });
});

HTML:

HTML:

<button id="fireme" class="btn btn-default">Fire me up!</button>
<div class="modal fade" id="EnSureModal" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">Do ypu need change </h4>
            </div>
            <div class="modal-body">
                <p>Are u sure from </p>
                <label id="FromDate"></label>
                <p>To</p>
                <label id="ToDate"></label>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">no</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">yes</button>
            </div>
        </div>
    </div>
</div>

The fiddle

小提琴