【问题标题】:Javascript waiting result of modal模态的Javascript等待结果
【发布时间】:2015-11-05 08:19:17
【问题描述】:

我有一个关于 javascript 和引导模式的简单问题。我有一个使用文件上传表单的向导,所以我检查文件的名称,如果它不包含一些字符串,我必须显示一个模式,询问你是否要继续。我如何知道从 javascript 或 jquery 中单击了哪个按钮进入模态? 我有这个代码:

if (index==3){
    var fileControl = document.getElementById('file');
    //Check if the datatable name contains idFLeet and IdCar. REturn -1 if it not contains the string
    if (fileControl.files[0].name.indexOf($("#selectedFleet").val()) > -1 && fileControl.files[0].name.indexOf($("#selectedCar").val()) > -1){
        //File contains id so you can continue the upload
        uploadFunction();
    }
    else{
        $('#warningDatatableModal').modal("show");  
         //If click on Yes call uploadFunction
        //If click on No return false
    }
}

HTML 模态代码:

<div class="modal" id="warningDatatableModal" data-backdrop="static" data-keyboard="false">
    <div class="modal modal-warning">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"
                        aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                    <h4 class="modal-title">Warning</h4>
                </div>
                <div class="modal-body">
                    <p>There is a incongruity between file name and fleet and
                        car previously selected. Are you sure to continue?</p>
                </div>
                <div class="modal-footer">
                    <button id="close" type="button" class="btn btn-outline pull-left"
                        data-dismiss="modal">Close</button>
                    <button id="continueButton" type="button" class="btn btn-outline">Continue</button>
                </div>
            </div>
            <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
    </div>
</div>

在 Yeldar Kurmangaliyev 建议后,我更新了我的代码,但向导转到下一个选项卡而不是等待进入实际选项卡

if (index==3){
    var fileControl = document.getElementById('file');
    //Check if the datatable name contains idFLeet and IdCar. REturn -1 if it not contains the string
    if (fileControl.files[0].name.indexOf($("#selectedFleet").val()) > -1 && fileControl.files[0].name.indexOf($("#selectedCar").val()) > -1){
        //File contains id so you can continue the upload
        uploadFunction();
    }
    else{
        $('#warningDatatableModal').modal("show");  
        $(".modal-footer > button").click(function() {
            clicked = $(this).text();
            $("#warningDatatableModal").modal('hide');
        });
        $("#warningDatatableModal").on('hide.bs.modal', function() {
            if (clicked === "Cancel"){
                return false;
            }else {
                uploadFunction();
            }
        });
    }
}

如果我将此代码置于向导工作之外(它不会使用取消按钮关闭模式,因为我使用了向导所需的 return false),但我需要停止向导等待模式结果。

【问题讨论】:

  • 显示模态代码
  • 你有你的HTML,对吧?所以默认有两个按钮,一个是用于“取消”的(data-target =“close”(类似这样,不记得确切的属性)),用于关闭模式,第二个是“确定”没有任何绑定,您可以在该按钮上嵌套任何您想要的操作,只需使用默认选择器。 $('.modal-footer button#submit').click(function(){ alert(1); });
  • 我添加了 HTML 代码,带有 '$('.modal-footer button#submit').click(function(){ alert(1); });'当用户点击按钮时,我不会停止 javascript 的执行
  • 为您添加了解决方案

标签: javascript jquery twitter-bootstrap modal-dialog


【解决方案1】:

您可以简单地将事件附加到您的按钮。
然而,用户可能想要关闭窗口而不采取行动。模态 UI 及其覆盖的设计建议在窗口外单击并关闭它。

为了创建一个在用户关闭您的模式时总是触发事件的事件,您需要附加到hide.bs.modal 事件:

$('#warningDatatableModal').on('hide.bs.modal', function() {
});

这是工作的JSFiddle demo

【讨论】:

  • 我已将您的代码用作我的第一篇文章,但向导转到下一个选项卡,然后它等待模态输入,但我认为无法停止像 java 这样的 javascript 代码
【解决方案2】:

您可以尝试使用按钮的 ID 并附加一个侦听器以查看它们何时被单击,如下所示:

$('#close').on('click',function(){
    //do something
});

$('#continueButton').on('click',function(){
    //do something
});

【讨论】:

  • 使用这个解决方案,代码可以运行,我需要模态结果来决定是继续还是停止向导
猜你喜欢
  • 2019-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多