用于添加和编辑的一种模式引导程序 - Upsert

我有 2 个链接(添加和编辑客户表单),它们触发一个相同的引导模式。我的问题是,同一个引导模式如何检测它是来自“添加链接”或“编辑链接”?


我当前的代码是


    <a href="" class="btn btn-default btn-rounded mb-4" data-toggle="modal" data-target="#modalCustomerForm">

        Add New Customer

    </a>


$('#modalCustomerForm').on('shown.bs.modal', function () {


    $(this).find('form')[0].reset();


})


白衣染霜花
浏览 80回答 1
1回答

慕运维8079593

使用对象relatedTarget的属性event来访问触发模态打开的元素$('#exampleModal').on('shown.bs.modal', function(e){&nbsp; &nbsp;const buttonId = e.relatedTarget.id;&nbsp; &nbsp;$(this).find('.modal-body').text(`Button id = ${buttonId}`);&nbsp; &nbsp; &nbsp;&nbsp;});<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"><script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" ></script><script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" ></script><!-- Button trigger modal --><button id="btn_1" type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">&nbsp; Modal Button 1</button><button id="btn_2" type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">&nbsp; Modal Button 2</button><!-- Modal --><div class="modal fade" id="exampleModal"&nbsp; aria-hidden="true">&nbsp; <div class="modal-dialog" role="document">&nbsp; &nbsp; <div class="modal-content">&nbsp; &nbsp; &nbsp; <div class="modal-header">&nbsp; &nbsp; &nbsp; &nbsp; <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>&nbsp; &nbsp; &nbsp; &nbsp; <button type="button" class="close" data-dismiss="modal" aria-label="Close">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span aria-hidden="true">&times;</span>&nbsp; &nbsp; &nbsp; &nbsp; </button>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; <div class="modal-body"></div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; </div>&nbsp; </div></div>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript