gpt4 book ai didi

javascript - 将变量传递给 Bootstrap 模式

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:16:53 25 4
gpt4 key购买 nike

我有一个 javascript 确认/取消弹出框,它动态地取消选中一个复选框,并且还动态地隐藏或显示一个 menu_entry,它是出现在表单上的列表的一部分,只有当用户选择 js 确认上的确定按钮时弹出框。

我现在正尝试将 js 确认更改为 Bootstrap 模式,但我不确定如何将变量传递给 Bootstrap 模式代码并获得与 js 确认弹出框相同的功能。

这是我的工作 html 代码,它调用了 javascript 确认弹出框:

{% for id, menu_entry, selected, item_count in menu_entries %}

<div class="available_resume_details_height">
<input type="checkbox"
style="width:1.5em; height:1.5em;"
name="selected_items"
value="{{ id }}"
{% if selected %}checked="checked"{% endif %}

onclick="checkboxUpdated(this, {{ item_count }}, '{{ menu_entry.label }}', {{ id }});"
/>

<span style="cursor: pointer;"
rel="popover"
data-content="{{ menu_entry.tooltip }}"
data-original-title="{{ menu_entry.label }}"
{{ menu_entry.label }}
</span>

</div>

{% endfor %}

这是我的 javascript 代码,用于显示确认弹出窗口并取消选中复选框并显示/隐藏列表条目:

function checkboxUpdated(checkbox, count, label, id) {
if (checkbox.checked) {
$('#menu_entry_' + id).show();
} else {
if (count > 0) {
if (! confirm('{% trans "You have '+ count +' saved '+ label +'.\n\nAre you sure you want to delete your ' + count + ' saved ' + label +'?" %}')) {
checkbox.checked = true;
return;
}
}
$('#menu_entry_' + id).hide();
}
}

这是调用 Bootstrap 模式的新 html 代码。这确实使 Bootstrap 模式出现:

{% for id, menu_entry, selected, item_count in menu_entries %}

<div class="available_resume_details_height">
<input type="checkbox"
style="width:1.5em; height:1.5em;"
name="selected_items"
value="{{ id }}"
{% if selected %}checked="checked"{% endif %}

{% if selected %}
data-confirm="{% blocktrans with entry_label=menu_entry.label %}Are you sure you want to remove your {{ item_count }} selected {{entry_label}} Resume Details?{% endblocktrans %}"
{% endif %}

/>

<span style="cursor: pointer;"
rel="popover"
data-content="{{ menu_entry.tooltip }}"
data-original-title="{{ menu_entry.label }}"
{{ menu_entry.label }}
</span>

</div>

{% endfor %}

这是我的 Bootstrap 模式代码,它不起作用。我不确定如何传递变量并获得与 js 确认弹出窗口相同的功能:

    $(document).ready(function() {

$('input[data-confirm]').click(function(ev) {

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

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

$('body').append('<div id="dataConfirmModal" class="modal modal-confirm-max-width" role="dialog" aria-labelledby="dataConfirmLabel" 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="dataConfirmLabel">{% trans "Confirm" %} - {{ resume_detail_temp_value }}</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="dataConfirmOK">{% trans "Confirm" %}</a></div></div>');

}

$('#dataConfirmModal').find('.modal-body').html($(this).attr('data-confirm'));

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

$('#dataConfirmOK').click(function() {

// handle checkbox function here.

});

return false;
});

});

Bootstrap 模式应显示/隐藏列表条目,并且仅在选择确认按钮时取消选中复选框。

编辑:添加复选框代码:

<input type="checkbox" 
style="width:1.5em; height:1.5em;"
name="selected_items"
value="{{ id }}"
{% if selected %}checked="checked"{% endif %}
onclick="checkboxUpdated(this, {{ item_count }}, '{{ menu_entry.label }}', {{ id }});"/>

最佳答案

数据属性可以帮助您以无缝方式在函数中传递所需的数据,还可以帮助您避免维护不友好的内联 JavaScript。设置三个事件监听器如下:

function checkboxUpdated(checkbox, count, label, id) {
var checkbox = this,
count = $(checkbox).data('count'),
label = $(checkbox).data('label'),
id = checkbox.value;
if(checkbox.checked) {
$('#menu_entry_' + id).show();
} else {
if (count > 0) {
$('#confirm_modal').data('element', checkbox).find('div.modal-body p:first')
.html( 'You have ' + count + ' saved ' + label + '. Are you sure you want to delete your ' + count + ' saved ' + label + '?' ).end()
.modal('show');
return;
}
$('#menu_entry_' + id).hide();
}
}
$(function() {
$(':checkbox').on('change', checkboxUpdated).change();
$('.confirm-no').on('click', function() {
var element = $('#confirm_modal').data('element');
element.checked = true;
});
$('.confirm-yes').on('click', function() {
var element = $('#confirm_modal').data('element'),
id = element.value;
$('#menu_entry_' + id).hide();
});
});

DEMO

关于javascript - 将变量传递给 Bootstrap 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27199447/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com