首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何用jquery作为字符串创建html模板?

如何用jquery作为字符串创建html模板?
EN

Stack Overflow用户
提问于 2017-06-09 11:46:01
回答 2查看 273关注 0票数 1

我有一个动态模式作为字符串,当您单击按钮时,我正在创建这个模型。(我没有只使用js的任何模板框架)

所以我的html中有两个日期属性:data-open-hoursdata-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 1See agency 2打开。

您先单击哪一个it模式已打开?

因此,我想我的事件是错误的,或者我的js函数用于创建html模板。

我的错误在哪里?

抱歉,我的英语很差。

代码语言:javascript
运行
复制
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">&times;</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))
  }
});
代码语言:javascript
运行
复制
.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;
}
代码语言:javascript
运行
复制
<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>

CodePen演示

EN

Stack Overflow用户

回答已采纳

发布于 2017-06-09 12:06:20

你的问题就在这一行:

代码语言:javascript
运行
复制
!$(".agencyModal").length && $(document.body).append(html);

第一次返回false之后,新的html就永远不会添加了。

此外,在飞行中创造一个新的模式并不能消除旧的模式。

在添加新的模式之前,需要删除先前添加的模式:

代码语言:javascript
运行
复制
$('.agencyModal, .modal-backdrop').remove();
$( 'body' ).removeClass('modal-open');

代码语言:javascript
运行
复制
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">&times;</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))
  }
});
代码语言:javascript
运行
复制
.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;
}
代码语言:javascript
运行
复制
<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>

票数 1
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44456970

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档