我有一个动态模式作为字符串,当您单击按钮时,我正在创建这个模型。(我没有只使用js的任何模板框架)
所以我的html中有两个日期属性:data-open-hours
和data-closed-hours
,当我的模式打开时,我控制了这个日期。
如果你看看我的演示,你会看到issue.let我和你解释。
如果您首先单击See Agency 1
,请参阅代理1模式将创建为字符串,并将使用js打开。
因此,在关闭See Agency 1
之后,如果您打开See Agency 2
,您将看到See Agency 1
将打开。
但是,如果刷新页面,如果单击第一个See Agency 2
,那么只要关闭See agency 2
,就会看到see agency 1
和See agency 2
打开。
您先单击哪一个it模式已打开?
因此,我想我的事件是错误的,或者我的js函数用于创建html模板。
我的错误在哪里?
抱歉,我的英语很差。
var openHours;
var closedHours;
function agencyModal(modalName, modalWidth, modalHeight, openHours, closedHours) {
console.log("Open: " + openHours + " Closed hours: " + closedHours);
var html =
'<div class="modal fade agencyModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"><div class="modal-dialog" role="document" style="width:' + modalWidth + ';height:' + modalHeight + '"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>';
html = html + '<h4 class="modal-title" id="myModalLabel">' + modalName + "</h4></div>";
html = html + '<div class="modal-body">';
html = html + '<div class="row"><div class="col-lg-12"><input type="text" class="form-control" placeholder="Adınız ve Soyadınız" name="adsoyad"></div></div>';
html = html + '<div class="row"><div class="col-lg-12"><input type="text" class="form-control" placeholder="E-posta Adresiniz" name="adsoyad"></div></div>';
html = html + '<div class="row"><div class="col-lg-12"><input type="text" class="form-control tel-number" placeholder="Telefon Numaranız" name="telnumber"></div></div>';
html = html + '<div class="row"><div class="col-lg-6"><select class="when-call form-control"><option class="call-today">Call Today</option><option class="call-tomorrow">Call Tomorrow</option></select></div><div class="col-lg-6">';
html = html + '<select class="hour-call form-control">' + getOptions(openHours, closedHours, true) + '</select></div></div>';
html = html + '<div class="row"><div class="col-lg-12"><button type="button" class="btn btn-success">Gönder</button></div></div></div>';
html = html + '<div class="modal-footer"><button type="button" class="btn btn-popup-kapat" data-dismiss="modal">Kapat</button></div>';
html = html + '</div></div></div>';
// check length and append if it is not added before
!$(".agencyModal").length && $(document.body).append(html);
$(".agencyModal").modal();
}
$(document).on("click", ".open-agency", function() {
openHours = $(this).data("open-hours");
closedHours = $(this).data("closed-hours");
agencyName = $(this).data("name");
agencyModal(agencyName, "40%", "500px", openHours, closedHours);
});
function callNow() {
return '<option class="call-now">Hemen Ara</option>';
}
function getOptions(open, close, now) {
var options = now ? callNow() : '';
console.log(open, close, now);
// get open/close time as hours only
var start = open.split(':')[0];
var end = close.split(':')[0];
// using +start will convert to a base 10 number - avoiding the problem that numbers with a leading zero are octal numbers
var dt = new Date();
var time = dt.getHours()
// loop and add an option for each
for (var h = +start; h <= +end; h++) {
if (time < h && now == true) {
options += '<option>' + h + ':00 - ' + (h + 1) + ':00 Arası</option>'
} else if (!now) {
options += '<option>' + h + ':00 - ' + (h + 1) + ':00 Arası</option>'
}
}
return options;
}
$(document).on("change", ".when-call", function(event) {
// not the most efficient way, but you can always remove 'Call now', then add it back only if needed
$(".hour-call .call-now").remove();
$('.hour-call option').remove();
if ($('.call-today').is(':selected')) {
$('.hour-call').prepend(getOptions(openHours, closedHours, true));
} else {
$('.hour-call').prepend(getOptions(openHours, closedHours, false))
}
});
.open-agency {
border: 3px solid #ccc;
display: inline-block;
padding: 12px;
font-size: 14px;
margin: 100px;
cursor: pointer;
box-shadow: 0px 0px 10px #ccc;
}
.open-agency:hover {
background: #050505;
color: #fff;
border-color: #000;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<p class="open-agency" data-toggle="modal" data-target=".agencyModal" data-open-hours="09:00" data-closed-hours="22:00" data-name="Agency 1">See Agency 1</p>
<p class="open-agency" data-toggle="modal" data-target=".agencyModal" data-open-hours="07:00" data-closed-hours="20:00" data-name="Agency 2">See Agency 2</p>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
发布于 2017-06-09 12:06:20
你的问题就在这一行:
!$(".agencyModal").length && $(document.body).append(html);
第一次返回false之后,新的html就永远不会添加了。
此外,在飞行中创造一个新的模式并不能消除旧的模式。
在添加新的模式之前,需要删除先前添加的模式:
$('.agencyModal, .modal-backdrop').remove();
$( 'body' ).removeClass('modal-open');
var openHours;
var closedHours;
function agencyModal(modalName, modalWidth, modalHeight, openHours, closedHours) {
console.log("Open: " + openHours + " Closed hours: " + closedHours);
var html = '<div class="modal fade agencyModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"><div class="modal-dialog" role="document" style="width:' + modalWidth + ';height:' + modalHeight + '"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>';
html = html + '<h4 class="modal-title" id="myModalLabel">' + modalName + "</h4></div>";
html = html + '<div class="modal-body">';
html = html + '<div class="row"><div class="col-lg-12"><input type="text" class="form-control" placeholder="Adınız ve Soyadınız" name="adsoyad"></div></div>';
html = html + '<div class="row"><div class="col-lg-12"><input type="text" class="form-control" placeholder="E-posta Adresiniz" name="adsoyad"></div></div>';
html = html + '<div class="row"><div class="col-lg-12"><input type="text" class="form-control tel-number" placeholder="Telefon Numaranız" name="telnumber"></div></div>';
html = html + '<div class="row"><div class="col-lg-6"><select class="when-call form-control"><option class="call-today">Call Today</option><option class="call-tomorrow">Call Tomorrow</option></select></div><div class="col-lg-6">';
html = html + '<select class="hour-call form-control">' + getOptions(openHours, closedHours, true) + '</select></div></div>';
html = html + '<div class="row"><div class="col-lg-12"><button type="button" class="btn btn-success">Gönder</button></div></div></div>';
html = html + '<div class="modal-footer"><button type="button" class="btn btn-popup-kapat" data-dismiss="modal">Kapat</button></div>';
html = html + '</div></div></div>';
// check length and append if it is not added before
// !$(".agencyModal").length && $(document.body).append(html);
//
// remove previous modal if it exists
//
$('.agencyModal, .modal-backdrop').remove();
$( 'body' ).removeClass('modal-open');
//
// add the new one
//
$(document.body).append(html);
$('.agencyModal').modal();
}
$(document).on("click", ".open-agency", function() {
openHours = $(this).data("open-hours");
closedHours = $(this).data("closed-hours");
agencyName = $(this).data("name");
agencyModal(agencyName, "40%", "500px", openHours, closedHours);
});
function callNow() {
return '<option class="call-now">Hemen Ara</option>';
}
function getOptions(open, close, now) {
var options = now ? callNow() : '';
console.log(open, close, now);
// get open/close time as hours only
var start = open.split(':')[0];
var end = close.split(':')[0];
// using +start will convert to a base 10 number - avoiding the problem that numbers with a leading zero are octal numbers
var dt = new Date();
var time = dt.getHours()
// loop and add an option for each
for (var h = +start; h <= +end; h++) {
if (time < h && now == true) {
options += '<option>'+h+':00 - '+(h+1)+':00 Arası</option>'
} else if (!now) {
options += '<option>'+h+':00 - '+(h+1)+':00 Arası</option>'
}
}
return options;
}
$(document).on("change", ".when-call", function(event) {
// not the most efficient way, but you can always remove 'Call now', then add it back only if needed
$(".hour-call .call-now").remove();
$('.hour-call option').remove();
if ($('.call-today').is(':selected')) {
$('.hour-call').prepend(getOptions(openHours, closedHours, true));
} else {
$('.hour-call').prepend(getOptions(openHours, closedHours, false))
}
});
.open-agency {
border: 3px solid #ccc;
display: inline-block;
padding: 12px;
font-size: 14px;
margin: 100px;
cursor: pointer;
box-shadow: 0px 0px 10px #ccc;
}
.open-agency:hover {
background: #050505;
color: #fff;
border-color: #000;
}
.agencyModal .row{
margin-bottom:10px;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<p class="open-agency" data-toggle="modal" data-target=".agencyModal" data-open-hours="09:00" data-closed-hours="22:00" data-name="Podium Avm">See Agency</p>
<p class="open-agency" data-toggle="modal" data-target=".agencyModal" data-open-hours="07:00" data-closed-hours="20:00" data-name="Another Agency Name">See Agency 2</p>
https://stackoverflow.com/questions/44456970
复制相似问题