javascript - JQuery datepicker on click on label
问题描述
I want a JQuery datepicker to show when I click on a specific label. I have a HTML like this:
<label id="kezd_datum_label">Data</label><input style="display:none;" id="kezd_datum" />
And some following JQueries like these:
$( function() {
$( "#kezd_datum" ).datepicker({
dateFormat:"yy-mm-dd"
});
} );
$("#kezd_datum_label").click(function() {
$("#kezd_datum").datepicker("show");
});
And
$(document).ready(function() {
$( "#kezd_datum" ).datepicker({
dateFormat:"yy-mm-dd"
});
$("#kezd_datum_label").click(function() {
$("#kezd_datum").focus();
});
});
I tried to copy the codes from here jQuery datepicker on click of icon, and it was accepted and everyone says it works, but it does not for some reason. However, if I make input visible, the datepicker appears as it should. Just not if I click on that label.
If a put in a window.alert("something");
into any of the click functions, they also run and appear.
解决方案
Try to replace display: none
to opacity: 0;width: 0
to show the input and use for
to point to your input.
$(document).ready(function() {
$("#kezd_datum").datepicker();
});
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<label for="kezd_datum" id="kezd_datum_label">Data</label><input style="opacity: 0; width: 0" id="kezd_datum" />
推荐阅读
- typescript - 如何克隆接口但在打字稿中扩展字段类型?
- android - android compose 有 textSize 单位 em
- python - 在气流环境中运行但手动启动时,通过 pandas 获取 SQLalchemy 未完成
- javascript - Detect Unsupported Browsers and Disable animations
- android - Add implementation "androidx.car.app:app:1.1.0-alpha02" occurs Manifest merger failed
- flutter - Assertion failed: /C:/Flutter/sdk/flutter/packages/flutter/lib/src/widgets/will_pop_scope.dart:135:12 _route == ModalRoute.of(context) is not true
- docker - 在创建 keycloak 容器时获取“/opt/jboss/tools/docker-entrypoint.sh: line 165: DB_ADDR: unbound variable”
- memory - 通过 JDBC 连接器从大型 MySQL 表执行 spark parquet 表
- android - Android TargetSDK 30 包可见性限制不适用于“com.google.android.gms”?
- azure - 从 Azure 运行 Childrunbook 不起作用,但从 Powershell CLI 运行