简体   繁体   English

在没有刷新页面,jquery,ajax的ajax发布成功后重置datepicker

[英]Reset datepicker after success ajax post without refresh page, jquery, ajax

I have html like this 我有这样的HTML

<div class="col-md-3">
<div id="date" data-date-format="dd-mm-yyyy"> </div>
</div>
<input id="datehidden" type="text" name="berlaku" value="">

and javascript like this 和像这样的javascript

var day = new Date();
   $('#date').datepicker({
     inline: true,
     sideBySide: true,
     startDate: day,
    }).on('changeDate',function (e) {
      value = e.format('yyyy-mm-dd');
      $('#datehidden').val(value);
    });

when i finished input a data to database i need to reset the form blank even my datepicker, i just try this 当我完成向数据库的数据输入后,我需要重设空白表单,甚至我的日期选择器,我也要尝试一下

$('#date').datepicker({setDate: null});

it doesn't work, i search with google and stackoverflow still didn't see best answer. 它不起作用,我用谷歌搜索,stackoverflow仍然找不到最佳答案。 would you help me? 你能帮我吗?

You should use the datepicker's event to update the date to empty or some specific value: $('#date').datepicker("update", ''); 您应该使用datepicker的事件将日期更新为空或某些特定值: $('#date').datepicker("update", '');

In case you want to destroy the datepicker you can use : $('#date').datepicker("destry"); 如果要销毁日期选择器,可以使用: $('#date').datepicker("destry");

I haven't found a simple and nice way to do it, so what I actually do is this: 我还没有找到一种简单而好用的方法,所以我实际要做的是:

When I call the jQuery function from my HTML, I actually erase the content within the div (where the date input is located) and then I insert it once again with a jQuery .append() and well that works actually good! 当我从HTML中调用jQuery函数时,实际上是擦除了div(日期input所在的位置)中的内容,然后再次使用jQuery .append()插入了它,效果很好!

//erase the content of div date1
var div = document.getElementById('date1');
while(div.firstChild){
    div.removeChild(div.firstChild);
}

//erase the content of div date2
var div = document.getElementById('date2');
while(div.firstChild){
    div.removeChild(div.firstChild);
}

$("#date1").append("<span class='input-group-addon' data-toggle='tooltip' title='Fecha fin de la movilidad'><b>FF</b></span><input type='text' name='fecha_1_reg' id='fecha_1_reg' placeholder='Ej: 2017-01-31'/>");
$("#date2").append("<span class='input-group-addon' data-toggle='tooltip' title='Final date'><b>FF</b></span><input type='text' name='fecha_2_reg' id='fecha_2_reg' placeholder='Ej: 2017-01-31'/>");

In the HTML: 在HTML中:

<td align='center'>
    <div id="date1" class='input-group col-xs-3'>
        <span class='input-group-addon' data-toggle='tooltip' title='First date'><b>FI</b></span>
        <input type='text' name='fecha_1_reg' id='fecha_1_reg' placeholder='Ej: 2017-01-01' />
    </div>
</td>
<td align='center'>
    <div id="date2" class='input-group col-xs-3'>
        <span class='input-group-addon' data-toggle='tooltip' title='last date'><b>FF</b></span>
        <input type='text' name='fecha_2_reg' id='fecha_2_reg' placeholder='Ej: 2017-01-31' />
    </div>
</td>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM