twitter-bootstrap Twitter Bootstrap Datepicker 不会更新输入值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13591159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 15:48:15  来源:igfitidea点击:

Twitter Bootstrap Datepicker won't update input value

javascripttwitter-bootstrap

提问by intelis

I have this code, but now i am stuck:

我有这个代码,但现在我被卡住了:

<input type="text" id="datepicker" value="/">

<script type="text/javascript">
    $('#datepicker').datepicker()
        .on('changeDate', function(ev){
            // Some code..
        });
</script>

I want the value of input field to update every time the new date is selected. I want the value to be the date, that was selected.

我希望每次选择新日期时都更新输入字段的值。我希望该值是选择的日期。

采纳答案by intelis

$(document).ready(function(){

    var dp = $("#datepicker");

    dp.datepicker({
        format : 'mm-dd-yyyy',
    });

     dp.on('changeDate', function(ev){
       dp.val(ev.target.value);
    });

});

回答by Zorkind

I had to do some crazy stuff for mine to work. Something about version perhaps? i don't know.

为了我的工作,我不得不做一些疯狂的事情。也许是关于版本的?我不知道。

        var dt = $(".datepick");

        dt.datepicker({
            format: 'dd/mm/yyyy'
        });

        dt.on('changeDate', function (ev) {
            var mDate = new moment(ev.date);

            $("input", dt).attr('value',mDate.format('DD/MM/YYYY'));
        });

Yes, i am using moment js, it's because i am doing some date calculations on the page.

是的,我正在使用 moment js,这是因为我正在页面上进行一些日期计算。

But it worked.

但它奏效了。

I needed the input to have the value so i could use jquery selectors to filter empty fields with :not([value=''])selector.

我需要输入具有值,以便我可以使用 jquery 选择器来过滤带有:not([value=''])选择器的空字段。

I hope it helps some body ^_^

希望对身体有帮助^_^

回答by Timur Misharin

Html:

网址:

<div class="input-group input-daterange">
    <input type="text" class="form-control" value="2019-03-05">
    <div class="input-group-addon">to</div>
    <input type="text" class="form-control" value="2019-03-14">
</div>

JS:

JS:

$(".input-daterange input").each(function(){
        $(this).datepicker('setValue');
});

回答by Rahul Thakur

<script type="text/javascript">
$(function() {
    $( "#datepicker" ).datepicker().on('changeDate', function(ev){
        // Some code..
    });;
});

The date picker should be attache to the #datepicker element when the page is ready by putting it in

当页面准备好时,日期选择器应该附加到#datepicker 元素上

$(function() {
  //code
}

or

或者

$(document).ready($function{
 //some code
});

or make sure that

或确保

$("#datepicker").datepicker().on('changeDate', function(ev){}); 

is executed in a function before the #datepicker element is clicked on

在点击#datepicker 元素之前在函数中执行