Trigger jQueryUI datePicker on button click, send result to disabled input field

江枫思渺然 提交于 2019-11-27 02:47:45

问题


I've been searching for a way to do this, but could not find anything

I want to have a:

  • Disabled input text field, called #XX. This input will store the selected value from datepicker (as this input is disabled I won't be able to use this to trigger the datePicker)

  • Button, besides #XX. When user clicks this button, datePicker will show. User will select the date, and this date will be assigned to disabled input #XX.

I want this so user can't change manually -by typing crap- the selected date

Also, should I validate if date was entered using #XX.val()? or is there a better way?


回答1:


<script type="text/javascript">
    $(function () {
        $("#datepicker").datepicker({
            constrainInput: true,
            showOn: 'button',
            buttonText: 'Select...'
        });
    });
</script>
<input id="datepicker" disabled="disabled" />

This will add a button next to the input field, and disable the user from entering text into the field...




回答2:


You can show a datepicker that has been previously attached to an input element using the "show" method.

$('#myButton').click(function () {
    $('#myField').datepicker("show");
});



回答3:


Might be to late but I just did this

$("#firstDate").datepicker({
    onSelect: function () {
        myfunc();
    }
}); 

$("#secondDate").datepicker({
    showOn: ''
}); 

function myfunc(){
    var fDate = $('#firstDate');
    var sDate = $('#secondDate');
    sDate.val(fDate.val());
}

It doesn't show the datepicker clicking the input and doesn't display the button to show the datepicker. Disabled doesn't work in my case because I need to extract the value from that input field.



来源:https://stackoverflow.com/questions/2611515/trigger-jqueryui-datepicker-on-button-click-send-result-to-disabled-input-field

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