jquery datepicker not working clicking on a button

廉价感情. 提交于 2019-12-13 03:35:27

问题


I am working on jquery datepicker.It is working on clicking on a button but only in mozilla firefox but not in google chrome not in IE too.Please tell me what to do so that the datepicker will work in all browsers.Please the jsfiddle

HTML

<input type="button" id="selectdate" value=""> 


<textarea name="dates" id="dates" maxlength="160" placeholder="your message" rows="4" cols="50"></textarea> 

JQUERY

$( "#selectdate" ).datepicker({ 
altField: $('#dates').val(), 
numberOfMonths:1, 
altFormat: "yy-mm-dd", 
minDate: -0, 
onSelect: function( selectedDate ) { 
$("#selectdate").val(''); 

$('#dates').val($('#dates').val()+','+selectedDate); 
} 
}); 

回答1:


Try following..

Demo Fiddle

JS:

$("#selectdate").datepicker({
    altField: $('#dates').val(),
    numberOfMonths: 1,
    altFormat: "yy-mm-dd",
    minDate: -0,
    buttonImage: 'http://placehold.it/32',
    showOn: "both",
    onSelect: function (selectedDate) {
        $("#selectdate").val('');

        var attr = $("#dates").attr("selectedDate");
        if (typeof (attr) !== 'undefined' && attr !== '') {
            if ($('#dates').val() !== '') {
                var tmpVal = $('#dates').val().substring(0,$('#dates').val().indexOf(attr));
               console.log(tmpVal); 
              $('#dates').val(tmpVal + selectedDate);
              $('#dates').attr("selectedDate", selectedDate);
            } else {
                $('#dates').val(selectedDate);
            }
        } else {
            $('#dates').attr("selectedDate", selectedDate);
            $('#dates').val($('#dates').val() + selectedDate);
        }
    }
});



回答2:


Not able to recreate the problem... but I think what you need is to use the showOn and buttonImage options instead of using a button to render the datepicker

like

<input type="text" id="selectdate" value="" style="display: none">

then

$("#selectdate").datepicker({
    altField: $('#dates').val(),
    numberOfMonths: 1,
    altFormat: "yy-mm-dd",
    minDate: -0,
    buttonImage:'http://placehold.it/32',
    showOn: "both",
    onSelect: function (selectedDate) {
        $("#selectdate").val('');

        $('#dates').val($('#dates').val() + ',' + selectedDate);
    }
});

Demo: Fiddle



来源:https://stackoverflow.com/questions/19580797/jquery-datepicker-not-working-clicking-on-a-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!