使用带有jquery的datepicker,与div onselect相关联的对话框

时间:2016-01-07 02:58:44

标签: javascript jquery datepicker onselect

不确定这是否可行,我刚开始学习javascript和jQuery。如果我不希望这样的方式,我很乐意听到不同的方式来实现这一目标。

我想显示一个日期选择器,用户将点击日期,当他们进行操作时,会出现一个对话框,其中包含特定的预定义数据。他们可以选择不同的日期,它会打开另一个包含不同预定义数据的对话框,我想跟踪他们点击的日期。

<div id="tabs">
 <ul>
<li><a href="#tabs-1">First</a></li>
<li><a href="#tabs-2">Second</a></li>
<li><a href="#tabs-3">Third</a></li>
</ul>
<div id="tabs-1">
<p>I'm going to have different data applying to only this day</p>
</div>
<div id="tabs-2">
<p>I'm going to have different data applying to only this day</p>
</div>
<div id="tabs-3">
<p>I'm going to have different data applying to only this day</p>
</div>
</div>

Open jQuery dialog box upon selecting a date from jQuery datepicker inline

该链接非常有用,因为用户有一个jsfiddle:http://jsfiddle.net/qqabC/这是我想要做的开始,我只是不确定如何或是否甚至可以将div合并到像这样的对话框。我一直在搞乱,但我一直无处可去。选择的每个日期都将包含不同的对话框内容。

如果无法做到这一点,那么实现这一目标的最佳方法是什么?谢谢。

- 编辑: 我仍然需要使用div id“tabs”来实现我在上面定义的div。标签=锻炼日。因此,tab-1到tabs-5将是5天。 tabs-1将是他们的第一个单击,即第1天表单内容,tabs-2将是他们的第二个单击,即第2天表单内容,依此类推。我相信我应该使用for循环,因为每次单击div都会增加到下一个。我试图做一些像:

 var divs = $('#tabs > div[id]');
 var links = $('#tabs li');
 divs.hide();
 for (i=0;i<=max_workouts;i++) {
     $('#tabs li').on('click', function(e){
     var clickedID = $(this).attr('href').clone().appendTo(#workout-modal);
     }

类似的东西,每次点击迭代div,但它不起作用,我一直试图找到将现有div内容放在模态中的例子但是没有什么以这种方式迭代div,你有没有有什么建议吗?

1 个答案:

答案 0 :(得分:0)

以下是如何使用Bootstrap实现此功能的示例。当然,您需要根据需要更改功能和设计,但这应该是一个良好的开端

工作流程如下:

  • 用户点击日期
  • 显示各种输入的模态
    • 模态打开时清除输入
  • 用户在输入中输入信息
  • 用户点击添加锻炼
  • 在显示屏上添加一个跨度标签,显示锻炼号码和所选日期
    • 此范围的数据属性设置为存储日期,标题和模态输入中的每个值
  • 如果用户点击了跨度右端的“X”,则会将其删除
  • 如果用户在任何其他位置单击span标签,则会重新打开模式,并使用作为跨度上的属性存储的数据填充输入
  • 如果用户在加载现有版本后点击添加锻炼,则新版本将替换旧版本,而不是添加到div的末尾

Here is a jsFiddle also

$(function(){
    var max_workouts = 5;
    $('#workout-datepicker').datepicker({
      startDate: "today"
    }).on('changeDate', function(e) {
      var cur = $('.workout-label').length;
      if (cur < max_workouts) {
        var workoutDate = e.format('mm/dd/yyyy');
        var title = 'Workout ' + (cur + 1) + ' - ' + workoutDate;
        openModal(title, workoutDate);
      }
      else{
    	  var $tooMany=$('#too-many');
        $tooMany.show();
        setTimeout(function(){ $tooMany.hide() }, 2000);
      }
    });
    var $workoutLabelsContainer = $('#workout-labels-container');
    $('#add-workout').click(function() {
      var $workoutModal = $('#workout-modal');
      var workoutDate = $workoutModal.data('workout-date');
      var title = $workoutModal.data('workout-title');
      var vaule1 = $('#modal-workout-value-1').val();
      var vaule2 = $('#modal-workout-value-2').val();
      var $workout = $('<span class="label label-primary workout-label col-sm-12">' + title + '<span class="glyphicon glyphicon-remove pull-right remove-workout" aria-hidden="true"></span></span>');
      var clickedLabelIndex = $workoutModal.data('crurent-label-index');
      $workout.data('workout-title', title).data('workout-date', workoutDate).data('value-1', vaule1).data('value-2', vaule2);
      if (clickedLabelIndex == -1) $workoutLabelsContainer.append($workout);
      else($('.workout-label').eq(clickedLabelIndex).replaceWith($workout))
      $workoutModal.modal('hide');
    });
    $workoutLabelsContainer.on('click', '.remove-workout', function(e) {
      e.stopPropagation();
      $(this).closest('.workout-label').remove();
    })
    $workoutLabelsContainer.on('click', '.workout-label', function() {
      var $workoutLabel = $(this);
      var workoutDate = $workoutLabel.data('workout-date');
      var title = $workoutLabel.data('workout-title');
      var value1 = $workoutLabel.data('value-1');
      var value2 = $workoutLabel.data('value-2');
      var labelIndex = $('.workout-label').index($workoutLabel);
      openModal(title, workoutDate, value1, value2, labelIndex);
    });

    function openModal(title, workoutDate, value1, value2, labelIndex) {
      var $workoutModal = $('#workout-modal');
      var $value1 = $('#modal-workout-value-1').val('');
      var $value2 = $('#modal-workout-value-2').val('');
      $workoutModal.data('workout-title', title).data('workout-date', workoutDate);
      $('#workout-modal-title').html(title);
      if (value1) $value1.val(value1);
      if (value2) $value2.val(value2);
      if (labelIndex !== 'undefined' && labelIndex > -1) $workoutModal.data('crurent-label-index', labelIndex);
      else $workoutModal.data('crurent-label-index', -1);
      $workoutModal.modal({
        show: true
      });
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.5.1/js/bootstrap-datepicker.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.5.1/css/bootstrap-datepicker.min.css" rel="stylesheet"/>
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<style>
  .workout-label {
  padding: 6px;
  font-size: 16px;
  width: 100%;
  display: block;
  margin-bottom: 5px;
  cursor: pointer;
}

.remove-workout {
  cursor: pointer;
}

#too-many{
  display:none;
}
</style>
<br>
<br>
<div class="container well" id="workout-container">
  <div class="row">
    <div class="col-xs-6">
      <div id="workout-datepicker"></div>
    </div>
    <div class="col-xs-6" id="workout-labels-container">
    </div>
  </div>
<div class="alert alert-danger" id="too-many" role="alert">Maximun reached</div>
</div>
<div id="workout-modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
        <h3 id="workout-modal-title"></h3>
      </div>
      <div class="modal-body">
        <form class="form-horizontal" role="form">

          <div class="form-group">
            <label for="firstname" class="col-sm-4 control-label">Some short text:</label>
            <div class="col-sm-8">
              <input type="text" class="form-control" id="modal-workout-value-1" placeholder="">
            </div>
          </div>

          <div class="form-group">
            <label for="lastname" class="col-sm-4 control-label">Some longer text:</label>

            <div class="col-sm-8">
              <textarea class="form-control" id="modal-workout-value-2" name="textarea"></textarea>
            </div>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
            <button type="button" class="btn btn-primary" id="add-workout">Add workout</button>
          </div>
        </form>
      </div>
    </div>
  </div>