简体   繁体   English

使用Twitter Bootstrap模式按钮时获取e.relatedTarget

[英]Get e.relatedTarget when using Twitter Bootstrap modal buttons

I have written the code to show a modal when a button is clicked, with the intention to edit button's text inside the modal. 我编写了代码,用于在单击按钮时显示模态,目的是在模态内编辑按钮的文本。

My intention is to have a lot of buttons, so instead of getting them by id, I use e.relatedTarget when the modal is invoked in order to know which button has called and get the text from the button so I can show it at the modal. 我的意图是有很多按钮,所以不是通过id获取它们,而是在调用模态时使用e.relatedTarget以便知道哪个按钮已调用并从按钮获取文本以便我可以在模态。

But the problem comes when I edit the text at the modal and click the OK button. 但是当我在模态下编辑文本并单击“确定”按钮时,问题就出现了。 I don't know how to access the e.relatedTarget from there. 我不知道如何从那里访问e.relatedTarget。

HTML: HTML:

<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">Change this text</button>
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <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" id="exampleModalLabel">New message</h4>

            </div>
            <div class="modal-body">
                <form>
                    <div class="form-group">
                        <label for="text-to-edit" class="control-label">Edit text:</label>
                        <input type="text" class="form-control" id="text-to-edit">
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary">Send message</button>
            </div>
        </div>
    </div>
</div>

JS: JS:

$('#exampleModal').on('show.bs.modal', function (event) {
    var button = $(event.relatedTarget) // Button that triggered the modal
    var buttonText = button.text() // Get text
    // Update the modal's content. We'll use jQuery here, but you could use a data binding library or other methods instead.
    var modal = $(this)
    modal.find('.modal-body input').val(buttonText)
});

// Modal ok button
$('#exampleModal .btn-primary').click(function() {
    // Get input text value
    var text = $('#exampleModal').find('.modal-body input').text();

    // Hide modal
    $('#exampleModal').modal('hide');

    // Set new text to the caller button
    // ?
});

JsFiddle: http://jsfiddle.net/nm4nmxsf/ JsFiddle: http//jsfiddle.net/nm4nmxsf/

Any help? 有帮助吗?

I added comments showing you what to change 我添加了评论,告诉你要改变什么

// Set an empty variable for button outside of your function !important
var button = '';

$('#exampleModal').on('show.bs.modal', function (event) {

    //don't redeclare your variable for button, instead update it
    button = $(event.relatedTarget) // Button that triggered the modal

    var buttonText = button.text() // Get text
    // Update the modal's content. We'll use jQuery here, but you could use a data binding library or other methods instead.
    var modal = $(this);
    modal.find('.modal-body input').val(buttonText);
});

// Modal ok button
$('#exampleModal .btn-primary').click(function() {
    // Get input text value

    //use val() instead of text()
    var text = $('#exampleModal').find('.modal-body input').val();

    $('#exampleModal').modal('hide');

    // Set new text to the caller button
    // ?
    button.text(text);
});

See this fidde: http://jsfiddle.net/nm4nmxsf/4/ 看到这个fidde: http//jsfiddle.net/nm4nmxsf/4/

var button;

$('#exampleModal').on('show.bs.modal', function (event) {
    button = $(event.relatedTarget);
    var buttonText = button.text();
    var modal = $(this);
    modal.find('.modal-body input').val(buttonText);
});

$('#exampleModal .btn-primary').click(function() {
    var text = $('#exampleModal').find('.modal-body input').val();
    $('#exampleModal').modal('hide');
   button.text(text);
});

JSFIDDLE 的jsfiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM