【问题标题】:Canceled Bootstrap modal still triggers canceled action when reopened (with class)已取消的引导模式在重新打开时仍会触发已取消的操作(带类)
【发布时间】:2017-09-12 01:57:24
【问题描述】:

以下代码显示了一个文档类型列表,其中每个类型都有一个按钮,允许上传文档并将文档类型用作变量(用于不同目的)。

每个按钮上的modal-addDoc 类触发打开带有表单的模式,以便收集需要与文件一起记录的信息。


问题:

当单击某个文档类型的按钮,然后取消模式(用户打开了错误类型的文档),然后单击另一个按钮,然后单击模式中的“注册者”按钮,操作对于取消的类型和最新的类型都被触发。出于演示目的,在下面的 sn-p 中,该操作只是一个警报。

例子:

  • 点击 CRANAP 行中的绿色按钮
  • 点击“annuler”(=取消)
  • 点击 CRHOSP 行中的绿色按钮
  • 点击“注册者”

您将收到 2 个警报:一个用于 CRANAP,另一个用于 CRHOSP。

我需要了解为什么仍然会触发第一个警报(因为我有更多具有相同结构和问题的代码)。

欢迎提出任何想法!

$('.modal-addDoc').click(function() {
    var uidTypeDoc = $(this).attr('data-uidTypeDoc');
    var uidQuest = $(this).attr('data-uidQuest');
    $("[id^=addDoc]").attr("id","addDoc-"+uidQuest+"-"+uidTypeDoc);
    $('#modal-addDoc').modal();
    $("#uidTypeDoc").text(" de type "+uidTypeDoc);
    var addDocId = "addDoc-"+uidQuest+"-"+uidTypeDoc;
    console.log(addDocId);
    $("#"+addDocId).click(function() {
        alert(addDocId)
    })
});
<head>
	<title>Dossier de l'enquête n°2038 - PRIMEV'R</title>

	<meta http-equiv="Content-type" content="text/html" charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">

	<link type="text/css" rel="stylesheet" href="https://primevr-preprod.constances.fr/_assets/bootstrap/css/bootstrap.min.css">

		<link type="text/css" rel="stylesheet" href="https://primevr-preprod.constances.fr/_dataTools/css/dataTools.css">
			<link type="text/css" rel="stylesheet" href="https://primevr-preprod.constances.fr/_dataTools/css/with-materialadmin.css">
	
	<link type="text/css" rel="stylesheet" href="https://primevr-preprod.constances.fr/_dataTools/css/primevr.css">

	<script type="text/javascript" src="https://primevr-preprod.constances.fr/_assets/jquery/jquery-2.2.4.min.js"></script>

	<script type="text/javascript" src="https://primevr-preprod.constances.fr/_assets/bootstrap/js/bootstrap.min.js"></script>
	
<style> button {height:30px}</style></head>
<body>
<table class="table table-bordered table-stripped no-margin">
            <thead>
                <tr>
                    <th colspan="7">Questionnaire n° 2038_1 : <em class="text-default-light"><small>aucune pathologie confirmée</small></em></th>
                </tr>
            </thead>
            <tbody>
                                    <tr>
                        <td class="style-default" style="border-bottom: 1px solid white">
                            <button type="button" class="btn btn-success btn-xs modal-addDoc" title="Ajouter un document CRANAP" data-uidquest="2038_1" data-uidtypedoc="CA_CRANAP">
                                <i class="fa fa-arrow-circle-down"></i>
                            </button>
                        </td>
                        <td colspan="4" class="style-default" style="border-bottom: 1px solid white">
                            <strong>CR d'anatomopathologie (ponction, biopsie, myélogramme)</strong> <span>(CRANAP)</span></td>
                        <td colspan="2" class="style-default align-right" style="border-bottom: 1px solid white">
                            </td>
                    </tr>
                                        <tr>
                        <td class="style-default" style="border-bottom: 1px solid white">
                            <button type="button" class="btn btn-success btn-xs modal-addDoc" title="Ajouter un document CRHOSP" data-uidquest="2038_1" data-uidtypedoc="CA_CRHOSP">
                                <i class="fa fa-arrow-circle-down"></i>
                            </button>
                        </td>
                        <td colspan="4" class="style-default" style="border-bottom: 1px solid white">
                            <strong>CR d'hospitalisation</strong> <span>(CRHOSP)</span></td>
                        <td colspan="2" class="style-default align-right" style="border-bottom: 1px solid white">
                            </td>
                    </tr>
                                        <tr>
                        <td class="style-default" style="border-bottom: 1px solid white">
                            <button type="button" class="btn btn-success btn-xs modal-addDoc" title="Ajouter un document CROAPP" data-uidquest="2038_1" data-uidtypedoc="CA_CROAPP">
                                <i class="fa fa-arrow-circle-down"></i>
                            </button>
                        </td>
                        <td colspan="4" class="style-default" style="border-bottom: 1px solid white">
                            <strong>CR d'opération, analyse pathologique de la pièce opératoire</strong> <span>(CROAPP)</span></td>
                        <td colspan="2" class="style-default align-right" style="border-bottom: 1px solid white">
                            </td>
                    </tr>
                                        <tr>
                        <td class="style-default" style="border-bottom: 1px solid white">
                            <button type="button" class="btn btn-success btn-xs modal-addDoc" title="Ajouter un document CRRCPL" data-uidquest="2038_1" data-uidtypedoc="CA_CRRCPL">
                                <i class="fa fa-arrow-circle-down"></i>
                            </button>
                        </td>
                        <td colspan="4" class="style-default" style="border-bottom: 1px solid white">
                            <strong>CR de réunion de concertation pluridisciplinaire (RCP)</strong> <span>(CRRCPL)</span></td>
                        <td colspan="2" class="style-default align-right" style="border-bottom: 1px solid white">
                            </td>
                    </tr>
                                        <tr>
                        <td class="style-default" style="border-bottom: 1px solid white">
                            <button type="button" class="btn btn-success btn-xs modal-addDoc" title="Ajouter un document DOMATU" data-uidquest="2038_1" data-uidtypedoc="CA_DOMATU">
                                <i class="fa fa-arrow-circle-down"></i>
                            </button>
                        </td>
                        <td colspan="4" class="style-default" style="border-bottom: 1px solid white">
                            <strong>Dosages de marqueurs tumoraux (ex. PSA)</strong> <span>(DOMATU)</span></td>
                        <td colspan="2" class="style-default align-right" style="border-bottom: 1px solid white">
                            </td>
                    </tr>
                                            <tr>
                            <th width="1%"></th>
                            <th>Date du document</th>
                            <th>Nom</th>
                            <th>Date d'ajout</th>
                            <th>Utilisateur</th>
                            <th>Description</th>
                            <th width="1%"></th>
                        </tr>
                                      
                                </tbody>
        </table>

<div id="modal-addDoc" class="modal animated fadeInUp" data-easein="fadeInUp" data-easeout="fadeOutUp" tabindex="-1" role="dialog">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button class="close" data-dismiss="modal">×</button>
                <h3>Charger un document <span id="uidTypeDoc"></span></h3>
            </div>
            <div class="modal-body">

                <form id="form-addDoc" enctype="multipart/form-data" method="post">
                    <input name="uidEnq" value="2038" type="hidden">

                    <div class="row">
                        <div class="col-sm-6">
                            <label><strong>Choisir le document</strong>
                                <input required="" name="document" type="file">
                            </label>
                        </div>
                        <div class="col-sm-3 input-group date">
                            <div class="input-group-content">
                                <label><strong>Date du document</strong>
                                    <input required="" class="form-control" name="dateDoc" placeholder="(J)J/(M)M/(AA)AA" type="text">
                                </label>
                            </div>
                            <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        </div>
                    </div>

                    <div class="row margin-top margin-left">
                        <label style="width:90%"><strong>Description</strong>
                            <textarea name="descr" rows="5" class="col-sm-12"></textarea>
                        </label>
                    </div>
                </form>
                <progress id="progress" style="display:none;"></progress>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-success" id="addDoc">Enregistrer</button>
                <button type="button" class="btn btn-default" data-dismiss="modal" aria-hidden="true">Annuler</button>
            </div>
        </div>
    </div>
</div>
</body>

【问题讨论】:

  • 看起来您只是在隐藏模式,而不是删除它。
  • 嘿@freginold thks 输入。你是什​​么意思“删除它”?我无法删除 html 元素,因为如果单击另一个下载按钮仍然需要它...?-|
  • 如果您无法删除包含模态的 HTML 元素,也许您可​​以在添加新模态之前将其清除。现在的方式是隐藏第一个(取消) 模式,但功能仍然存在。
  • 那么“清除”会是什么?我试图重置表单,但是由于没有使用表单,所以正如 sn-p 显示的那样,这不是表单的问题
  • 完全披露,我为这个项目做出了贡献,但是:您可能会发现类似 Bootbox 的内容(特别是自定义对话框)很有用 - 每次显示对话框时(默认情况下)都会重新创建模态,因此您不会遇到过时数据的问题。

标签: jquery bootstrap-modal


【解决方案1】:

发生这种情况时您会收到多个 alert 操作的原因是因为未删除先前选择的选项中的点击处理程序。所以每次选择一个选项时,都会添加一个新的点击处理程序,但不会删除旧的:

$("#"+addDocId).click(function() {
    alert(addDocId)
})

您所要做的就是在添加点击处理程序之前插入一个.off() 语句,以删除任何先前的处理程序/侦听器。请看这个例子,它只为当前选择的选项显示alert

$('.modal-addDoc').click(function() {
    var uidTypeDoc = $(this).attr('data-uidTypeDoc');
    var uidQuest = $(this).attr('data-uidQuest');
    $("[id^=addDoc]").attr("id","addDoc-"+uidQuest+"-"+uidTypeDoc);
    $('#modal-addDoc').modal();
    $("#uidTypeDoc").text(" de type "+uidTypeDoc);
    var addDocId = "addDoc-"+uidQuest+"-"+uidTypeDoc;
    console.log(addDocId);
    $("#"+addDocId).off();      // remove any previously-added listeners
    $("#"+addDocId).click(function() {
        alert(addDocId);
    });
});
<head>
	<title>Dossier de l'enquête n°2038 - PRIMEV'R</title>

	<meta http-equiv="Content-type" content="text/html" charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">

	<link type="text/css" rel="stylesheet" href="https://primevr-preprod.constances.fr/_assets/bootstrap/css/bootstrap.min.css">

		<link type="text/css" rel="stylesheet" href="https://primevr-preprod.constances.fr/_dataTools/css/dataTools.css">
			<link type="text/css" rel="stylesheet" href="https://primevr-preprod.constances.fr/_dataTools/css/with-materialadmin.css">
	
	<link type="text/css" rel="stylesheet" href="https://primevr-preprod.constances.fr/_dataTools/css/primevr.css">

	<script type="text/javascript" src="https://primevr-preprod.constances.fr/_assets/jquery/jquery-2.2.4.min.js"></script>

	<script type="text/javascript" src="https://primevr-preprod.constances.fr/_assets/bootstrap/js/bootstrap.min.js"></script>
	
<style> button {height:30px}</style></head>
<body>
<table class="table table-bordered table-stripped no-margin">
            <thead>
                <tr>
                    <th colspan="7">Questionnaire n° 2038_1 : <em class="text-default-light"><small>aucune pathologie confirmée</small></em></th>
                </tr>
            </thead>
            <tbody>
                                    <tr>
                        <td class="style-default" style="border-bottom: 1px solid white">
                            <button type="button" class="btn btn-success btn-xs modal-addDoc" title="Ajouter un document CRANAP" data-uidquest="2038_1" data-uidtypedoc="CA_CRANAP">
                                <i class="fa fa-arrow-circle-down"></i>
                            </button>
                        </td>
                        <td colspan="4" class="style-default" style="border-bottom: 1px solid white">
                            <strong>CR d'anatomopathologie (ponction, biopsie, myélogramme)</strong> <span>(CRANAP)</span></td>
                        <td colspan="2" class="style-default align-right" style="border-bottom: 1px solid white">
                            </td>
                    </tr>
                                        <tr>
                        <td class="style-default" style="border-bottom: 1px solid white">
                            <button type="button" class="btn btn-success btn-xs modal-addDoc" title="Ajouter un document CRHOSP" data-uidquest="2038_1" data-uidtypedoc="CA_CRHOSP">
                                <i class="fa fa-arrow-circle-down"></i>
                            </button>
                        </td>
                        <td colspan="4" class="style-default" style="border-bottom: 1px solid white">
                            <strong>CR d'hospitalisation</strong> <span>(CRHOSP)</span></td>
                        <td colspan="2" class="style-default align-right" style="border-bottom: 1px solid white">
                            </td>
                    </tr>
                                        <tr>
                        <td class="style-default" style="border-bottom: 1px solid white">
                            <button type="button" class="btn btn-success btn-xs modal-addDoc" title="Ajouter un document CROAPP" data-uidquest="2038_1" data-uidtypedoc="CA_CROAPP">
                                <i class="fa fa-arrow-circle-down"></i>
                            </button>
                        </td>
                        <td colspan="4" class="style-default" style="border-bottom: 1px solid white">
                            <strong>CR d'opération, analyse pathologique de la pièce opératoire</strong> <span>(CROAPP)</span></td>
                        <td colspan="2" class="style-default align-right" style="border-bottom: 1px solid white">
                            </td>
                    </tr>
                                        <tr>
                        <td class="style-default" style="border-bottom: 1px solid white">
                            <button type="button" class="btn btn-success btn-xs modal-addDoc" title="Ajouter un document CRRCPL" data-uidquest="2038_1" data-uidtypedoc="CA_CRRCPL">
                                <i class="fa fa-arrow-circle-down"></i>
                            </button>
                        </td>
                        <td colspan="4" class="style-default" style="border-bottom: 1px solid white">
                            <strong>CR de réunion de concertation pluridisciplinaire (RCP)</strong> <span>(CRRCPL)</span></td>
                        <td colspan="2" class="style-default align-right" style="border-bottom: 1px solid white">
                            </td>
                    </tr>
                                        <tr>
                        <td class="style-default" style="border-bottom: 1px solid white">
                            <button type="button" class="btn btn-success btn-xs modal-addDoc" title="Ajouter un document DOMATU" data-uidquest="2038_1" data-uidtypedoc="CA_DOMATU">
                                <i class="fa fa-arrow-circle-down"></i>
                            </button>
                        </td>
                        <td colspan="4" class="style-default" style="border-bottom: 1px solid white">
                            <strong>Dosages de marqueurs tumoraux (ex. PSA)</strong> <span>(DOMATU)</span></td>
                        <td colspan="2" class="style-default align-right" style="border-bottom: 1px solid white">
                            </td>
                    </tr>
                                            <tr>
                            <th width="1%"></th>
                            <th>Date du document</th>
                            <th>Nom</th>
                            <th>Date d'ajout</th>
                            <th>Utilisateur</th>
                            <th>Description</th>
                            <th width="1%"></th>
                        </tr>
                                      
                                </tbody>
        </table>

<div id="modal-addDoc" class="modal animated fadeInUp" data-easein="fadeInUp" data-easeout="fadeOutUp" tabindex="-1" role="dialog">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button class="close" data-dismiss="modal">×</button>
                <h3>Charger un document <span id="uidTypeDoc"></span></h3>
            </div>
            <div class="modal-body">

                <form id="form-addDoc" enctype="multipart/form-data" method="post">
                    <input name="uidEnq" value="2038" type="hidden">

                    <div class="row">
                        <div class="col-sm-6">
                            <label><strong>Choisir le document</strong>
                                <input required="" name="document" type="file">
                            </label>
                        </div>
                        <div class="col-sm-3 input-group date">
                            <div class="input-group-content">
                                <label><strong>Date du document</strong>
                                    <input required="" class="form-control" name="dateDoc" placeholder="(J)J/(M)M/(AA)AA" type="text">
                                </label>
                            </div>
                            <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        </div>
                    </div>

                    <div class="row margin-top margin-left">
                        <label style="width:90%"><strong>Description</strong>
                            <textarea name="descr" rows="5" class="col-sm-12"></textarea>
                        </label>
                    </div>
                </form>
                <progress id="progress" style="display:none;"></progress>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-success" id="addDoc">Enregistrer</button>
                <button type="button" class="btn btn-default" data-dismiss="modal" aria-hidden="true">Annuler</button>
            </div>
        </div>
    </div>
</div>
</body>

【讨论】:

  • 太棒了!它确实有效!为什么这样做是绝对神秘的:当时 $("#"+addDocId).off();运行时,addDocId 有新的doc type 的值,谁能移除上一个doctype 上的监听器???
  • 看来,虽然你改了ID,但还是同一个按钮,所以之前的点击处理程序被继承了。
  • 是的,你可能是对的,因为这也可以解释我正在努力解决的奇怪行为。不是很合乎逻辑的想法,可能是 jquery 人安排的东西……再次感谢您的输入
  • 嘿@freginold,我知道我不应该这样做,但我绝对坚持这个问题stackoverflow.com/questions/45863567/…不知道我仍然可以朝哪个方向前进看着,我也不明白为什么它只有 23 次观看。如果您只是忽略此消息,我会完全理解,但是任何关于如何让人们阅读它的线索、如何寻找解决方案或任何可以提供帮助的线索都将非常受欢迎!
  • 我做了一些改变,应该可以帮助它获得更多的浏览量;添加了“php”标签,更改了格式,并将您的代码变成了 sn-p。希望有想法的人看看。
猜你喜欢
  • 2020-05-16
  • 2020-08-13
  • 2022-01-16
  • 2021-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多