admin管理员组文章数量:1295634
I would like to display the datepicker when i click on the button. I have tried but i couldn't make it work. I am using Materialize Css. I have tried to use input type text but i don't want it.
<div class="container">
<div class="row center">
<i>Date</i>
<button class="btn waves-effect waves-light datepicker" type="submit" name="action">PICK DATE</button>
</div>
</div>
jQuery:
$('.datepicker').pickadate({
selectMonths: true,
selectYears: 15
});
I would like to display the datepicker when i click on the button. I have tried but i couldn't make it work. I am using Materialize Css. I have tried to use input type text but i don't want it.
<div class="container">
<div class="row center">
<i>Date</i>
<button class="btn waves-effect waves-light datepicker" type="submit" name="action">PICK DATE</button>
</div>
</div>
jQuery:
$('.datepicker').pickadate({
selectMonths: true,
selectYears: 15
});
Share
edited Apr 6, 2016 at 6:59
WhoAmI
5013 silver badges13 bronze badges
asked Apr 5, 2016 at 15:59
ryhan112ryhan112
511 gold badge2 silver badges8 bronze badges
0
3 Answers
Reset to default 6In order to show the datepicker from a button, you need to invoke the show option.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jDatapicker JQuery</title>
<script src="//code.jquery./jquery-1.10.2.js"></script>
<link rel="stylesheet" href="//code.jquery./ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery./ui/1.11.4/jquery-ui.js"></script>
<script>
$(function() {
$( "#datepicker" ).datepicker();
});
function show_dp(){
$( "#datepicker" ).datepicker('show'); //Show on click of button
}
</script>
</head>
<body>
<p>Date: <input type="text" id="datepicker"></p>
<button onclick="show_dp();">Show Datepicker</button>
</body>
</html>
Another option to make it so you don't need an input field:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jDatapicker JQuery</title>
<script src="//code.jquery./jquery-1.10.2.js"></script>
<link rel="stylesheet" href="//code.jquery./ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery./ui/1.11.4/jquery-ui.js"></script>
<script>
function toggle_dp() {
dp = $("#datepicker");
if (dp.attr('datepicker')) {
dp.datepicker('destroy');
dp.removeAttr('datepicker');
} else {
dp.datepicker();
dp.attr('datepicker', 1);
}
}
</script>
</head>
<body>
<div id="datepicker"></div>
<button id="button" onclick="toggle_dp();">Toggle Datepicker</button>
</body>
</html>
I think a better way is to only show the picker when the button is clicked and not when the input is clicked. This enables the user to edit the input manually as well.
My trick is to use a hidden input to connect the picker to:
<div class="input-field datepicker-prefix">
<i class="material-icons prefix">perm_contact_calendar</i>
<input type="hidden" class="datepicker" />
<input />
</div>
And then setup the click even to show the picker:
$('.datepicker-prefix .prefix').click(function () {
$(this).parent().find('.datepicker').datepicker('open');
});
See my plete solution for both date and time pickers https://codepen.io/dnote/pen/jXQNpg?editors=1010#0
You can use this code created by me
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jDatapicker JQuery</title>
<script src="//code.jquery./jquery-1.10.2.js"></script>
<link rel="stylesheet" href="//code.jquery./ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery./ui/1.11.4/jquery-ui.js"></script>
<script>
$(function() {
$( "#datepicker" ).datepicker();
});
</script>
</head>
<body>
<p>Date: <input type="text" id="datepicker"></p>
</body>
</html>
本文标签: javascriptDisplaying Datepicker when i click the buttonStack Overflow
版权声明:本文标题:javascript - Displaying Datepicker when i click the button - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741622334a2388883.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论