【发布时间】:2026-01-05 21:35:01
【问题描述】:
我正在尝试添加 Bootstrap Alerts,但无法以编程方式显示它们。
这是 HTML 部分:
<div
class="alert alert-success alert-dismissible fade show"
role="alert"
id="accepted-meal-alert">
<button type="button" class="close">×</button>
<p>Genial que te haya gustado la idea!</p>
</div>
CSS:
#accepted-meal-alert {
display: none;
}
还有 JavaScript。我正在尝试使用 jQuery 来切换 div 的可见性:
function acceptOrGetAnotherMeal(acceptOrReject) {
if (acceptOrReject == 'accept-btn') {
showMealAccepted();
} else {
const alternativeTextPrefix = 'Entonces pidamos';
let mealType = $("input[name='categorias']:checked").val();
console.log(`mealType: ${mealType}`);
getFromEndpoint(mealType, alternativeTextPrefix);
}
}
function showMealAccepted() {
console.log('showMealAccepted');
$('#accepted-meal-alert').show(); // `toggle` didn't work as well.
// $('#accepted-meal-alert').css({
// display: 'block',
// });
$('#accept-btn').prop('disabled', true);
$('#reject-btn').prop('disabled', true);
}
$(document).ready(function () {
$('.categorias').click(function () {
let endpoint = $(this).attr('id');
getFromEndpoint(endpoint, defaultTextPrefix);
});
$('.accept-reject-btn').click(function () {
let acceptOrReject = $(this).attr('id');
console.log('Clicked categories with value: ' + acceptOrReject);
acceptOrGetAnotherMeal(acceptOrReject);
});
$('.alert .close').click(function (e) {
$(this).parent().hide();
});
});
如果这有什么不同,我将从 Sinatra 公用文件夹中提供这些文件。
【问题讨论】:
-
虽然您可能正在使用 Sinatra,但这不是 Sinatra 编程问题,不应被标记。
标签: javascript jquery css twitter-bootstrap