【问题标题】:Steps to add datepicker to the bootstrap modal将日期选择器添加到引导模式的步骤
【发布时间】:2016-05-26 08:45:12
【问题描述】:

我正在做一个项目,我必须将 datepicker 添加到引导模式。我不知道如何将 datepicker 添加到模式弹出窗口。 我希望将日期选择器添加到引导模式的步骤没有任何错误。谁能指导我一步一步地为模态创建一个日期选择器。 以下是我的模态代码:

 <div class="modal fade" id="modal_calendar" tabindex="-1" role="dialog">
  <div class="modal-dialog">
    <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>
        <h4 class="modal-title">Modal title</h4>
      </div>
        <div class="modal-body">
         <form id="event_frm" class="form-horizontal">

         <div class="form-group">
    <label for="Title">Subject</label>
    <input type="text" class="form-control" id="event_Subject" placeholder="Subject">
   </div>
  <div class="form-group">
    <label for="body">Body</label>
       <input type="text" class="form-control" id="event_Body" placeholder="Body">
       </div>
      <div class="form-group">
    <label for="start_date">Body</label>
    <input type="text" class="form-control" id="event_start_date" placeholder="Start Date">
  </div>
        </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
              <button type="button" class="btn btn-primary" id="save_event">Save changes</button>
       </div>
      </div><!-- /.modal-content -->
   </div><!-- /.modal-dialog -->
 </div><!-- /.modal --> 

以下是我通过ajax保存表单数据的ajax调用:

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


var subject =$('#event_Subject').val();
var body =$('#event_Body').val();
var start_date = $('#event_start_date').val();
if($('#event_Subject').val() =='')
{
    alert('subject required'); return false;
}
else
{
    $("#modal_calendar").modal('hide');
}
if($('event_Body').val() == '')
{
    alert('Body required'); return false;
}
else
{
    $("#modal_calendar").modal('hide');
}
if($('event_start_date').val() == '')
{
    alert('Start Date required'); return false;
}
else
{
    $("#modal_calendar").modal('hide');
}    
$.ajax({
          cache: false,
          type: "POST",
          url:"calendar/save_event",
          data :  {'subject' : subject,'body' : body,'start_date' : start_date},
          dataType: 'json',
          success: function(result){
            if (result!=null){
                }
                }
                });
  });

    });

请帮助我,拯救我的一天。任何一步一步的代码都可以帮助我和像我这样的人..在此先感谢

【问题讨论】:

标签: ajax twitter-bootstrap jquery-ui-datepicker bootstrap-modal


【解决方案1】:

我建议您在您的源代码中使用 bootstrap-datepicker.js 和 bootstrap-datepicker.css。并按照以下步骤操作:

1).首先在你的源代码中添加js和css的库文件。 在 Html 中写为:

<div class="input-group date" id="datepickerDiv" style="width:75%; height:40px;">
    <input type="text" class="form-control" id="date_Input" style="color:#888; height:40px;">
    <div class="input-group-addon cal-addonImg">
        <span class="glyphicon glyphicon-th"></span>
    </div>
</div>

在 Js 中:当你专注于输入文本字段时

$("#date_Input").datepicker({
                autoclose:true,
                showOnFocus:true,
                format:'yyyy/mm/dd'
             });

希望对你有帮助……

【讨论】:

  • 使用引导日期选择器比其他选择器有什么优势?
  • 如果您在源代码中使用引导程序......并且使用它是可行的解决方案。在使用引导程序时....否则您可以使用 jquery 库等找到各种解决方案
  • 'Cannot read property 'hasTime' of undefined' 的真正含义是什么......?我收到此错误!
  • 所有头文件都加了吗?请告诉我
【解决方案2】:

使用 jQuery UI 可以非常轻松地将日期选择器添加到字段中。任何 jQuery 小部件,例如“datepicker”,都将作为一个函数存在,可以在页面上的任何元素上调用,如下所示: $('#event_start_date').datepicker();

这是一个带有您的代码和添加的日期选择器的 jsFiddle: https://jsfiddle.net/bishbashbosh/9jormmpz/1/

我不得不把代码放在一个

$(function() { . . . });

...以便在 jQuery 和 jQuery UI 加载后执行。

希望有帮助!

【讨论】:

  • 是的@bishbashbosh,非常感谢,我应该在css中添加任何修改吗?
  • 没问题 :) 不,您不需要任何额外的 CSS 或更改您的 HTML。您只需要确保在 HTML 中的 元素中有 jQuery UI CSS 文件。在我的 jsFiddle 中,我引用了 code.jquery.com/ui/1.12.0-rc.1/themes/smoothness/jquery-ui.css
  • 'Cannot read property 'hasTime' of undefined' 的真正含义是什么......?我收到此错误
  • 这意味着您编写的某些代码,或者您正在调用的某些代码具有 object 并且正在尝试调用方法(函数)或检索调用的属性(值) hasTime 来自该对象,但 object 未定义(即空变量)。如果您在错误发生时打开了 Chrome 开发人员工具,您应该能够在控制台中展开错误并找出它的来源,并可能找出它发生的原因。如果您仍然遇到问题,请将其记录为有关堆栈溢出的新问题,也许有人可以提供帮助。
猜你喜欢
  • 2020-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-31
  • 2014-06-29
  • 1970-01-01
相关资源
最近更新 更多