如何从函数调用Jquery

时间:2016-04-14 11:17:45

标签: javascript jquery html5 twitter-bootstrap bootstrap-modal

我有来自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>

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

1 个答案:

答案 0 :(得分:6)

我读了你的代码,我发现你没有正确使用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