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

前端之家收集整理的这篇文章主要介绍了javascript – 如何从函数调用Jquery打开模态前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有来自Bootstrap的Modal
当我添加
按钮要调用它,它会成功打开,
但我真正需要的是从函数调用自动打开这个模态
我的实验是:
<button id="RenewCollerctorDateID" class="btn btn-success" style="width: 10%; display: inline;
 padding-right: 10px; float: left;" onclick="RenewCollectorDatePeriod();">renew</button>

我的JavaScript是

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

然后:

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');
    }

而这里是我的模态

<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>

注意:当我添加$(“#EnSureModal”).modal(‘show’);在Document.Ready中,它在页面加载时出现并在函数调用中再次出现,如何才能使它出现在函数调用

解决方法

我读了你的代码,我发现你没有正确使用bootstrap,你写的脚本也不正确.看一下这个.这可能对你有帮助.

脚本:

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

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>

fiddle

原文链接:https://www.f2er.com/jquery/158060.html

猜你在找的jQuery相关文章