如果选中复选框,则显示 bootstrap 模态

display a bootstrap modal if checkbox is checked

我有一个测试页面,当用户单击表单按钮时,会出现 bootstrap 模式并向用户显示 yes/no 消息。它似乎工作正常。

但是,我一直在尝试更改代码,以便在用户选中复选框时显示/触发 bootstrap 模式 ,但我遇到了麻烦了解我的代码。

我希望有人能告诉我如何调整我的代码,以便选中的复选框触发模态显示。

这是我的脚本代码

<script>
    // START: photograph remove modal code.
    $('a[photograph-remove-confirm]').click(function(ev) {

        var href = $(this).attr('href');

        if (!$('#photographRemoveConfirmModal').length) {

            $('body').append('<div id="photographRemoveConfirmModal" class="modal modal-confirm-small-width hide fade" role="dialog" aria-labelledby="miscConfirmLabel" aria-hidden="true"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-hidden="true"><icon class="icon-remove"></icon></button><h4 class="modal-title" id="miscConfirmLabel">{% trans "Confirm" %} - {% trans "Remove" %} {% trans "Photograph" %}</h4></div><div class="modal-body"></div><div class="modal-footer"><button class="btn" data-dismiss="modal" aria-hidden="true">{% trans "Cancel" %}</button>&nbsp;&nbsp;<a class="btn-u btn-u-blue" id="photographRemoveConfirmOK">{% trans "Remove" %} {% trans "Photograph" %}</a></div></div>');

        }

        $('#photographRemoveConfirmModal').find('.modal-body').text($(this).attr('photograph-remove-confirm'));

        $('#photographRemoveConfirmOK').attr('href', href);

        $('#photographRemoveConfirmModal').modal({show: true});

        return false;

    });
    // FINISH: photograph remove modal code.
</script>

这是点击时显示模式的模板按钮代码:

<a href="#" id="test_button" class="btn" photograph-remove-confirm="Your Photograph will be removed only after you save the form!">{% trans "Test" %}</a>

这是我想用来触发模式的模板复选框代码:

<input type="checkbox" id="id_remove_photograph" name="remove_photograph" class="checkbox_resize"  onchange="remove_photograph_change();"> Remove Photograph</input>

这是上面复选框中显示的 onchange="remove_photograph_change" 函数。

function remove_photograph_change() {

    if ($('#id_remove_photograph').is(':checked')) {

        // just some code

    }

}

我认为以下更改将解决您遇到的问题。

将您的复选框代码更改为:

<input type="checkbox" id="id_remove_photograph" name="remove_photograph" class="checkbox_resize"  onchange="remove_photograph_change();" photograph-remove-confirm="Your Photograph will be removed only after you save the form!"> Remove Photograph</input>

将模态代码更改为:

    // START: photograph remove modal code.
    $('[photograph-remove-confirm]').click(function(ev) {

        if (!$('#photographRemoveConfirmModal').length) {

            $('body').append('<div id="photographRemoveConfirmModal" class="modal modal-confirm-small-width hide fade" role="dialog" aria-labelledby="miscConfirmLabel" aria-hidden="true"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-hidden="true"><icon class="icon-remove"></icon></button><h4 class="modal-title" id="miscConfirmLabel">{% trans "Confirm" %} - {% trans "Remove" %} {% trans "Photograph" %}</h4></div><div class="modal-body"></div><div class="modal-footer"><button class="btn" data-dismiss="modal" aria-hidden="true">{% trans "Cancel" %}</button>&nbsp;&nbsp;<a class="btn-u btn-u-blue" id="photographRemoveConfirmOK">{% trans "Remove" %} {% trans "Photograph" %}</a></div></div>');

        }

        $('#photographRemoveConfirmModal').find('.modal-body').text($(this).attr('photograph-remove-confirm'));

        $('#photographRemoveConfirmModal').modal({show: true});

        return false;

    });
    // FINISH: photograph remove modal code.