如何解决如何选择焦点上的值以进行选择?
我想为 selectize 选择焦点值。
此时,我可以单击并删除值,但我希望单击(焦点)应该选择 selectize 元素的所有值字符串。
您可以在创建新活动时了解 Google 日历的工作原理。
在我这边,如果我点击 time selectize 元素,那么它应该选择所有值字符串,并且可以删除选择或使用新字符串更新选择,例如 Google。
我的时间选择元素如下。
// php
<div class="select-beast is-hide-selectize-arrow select-event-time-end">
<select name='event_time_end' class='control is-normal select-time' required>
<?php echo TimeHelper::get_times(); ?>
</select>
</div>
// php
function get_times ($default = '19:00',$interval = '+30 minutes') {
$output = '';
$current = strtotime('00:00');
$end = strtotime('23:59');
while ($current <= $end) {
$time = date('H:i',$current);
$sel = ($time == $default) ? ' selected' : '';
$output .= "<option value=\"{$time}\"{$sel}>" . date('h:i A',$current) .'</option>';
$current = strtotime($interval,$current);
}
return $output;
}
...
//jquery
<script>
const $selected_time_start = $('.select-beast select[name="event_time_start"]').selectize({
create: true
});
const selectize_event_start = $selected_time_start[0].selectize;
</script>
我尝试使用焦点事件和 select method 如下所示。 但是没有用。
$('.select-beast select[name="event_time_start"]').focus(function() { $(this).select(); } );
解决方法
经过 3 个多小时的研究,我找到了最相关的答案。
我需要将 selectize 版本更新为最新版本并使用 select_on_focus
插件。
插件在这里。 https://github.com/selectize/selectize.js/tree/master/src/plugins/select_on_focus
使用方法:
如果您使用的是 jQuery,则可以使用 selectize 的独立版本。
...
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.13.3/js/standalone/selectize.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.13.3/css/selectize.min.css" rel="stylesheet">
</head>
<body>
...
<select class="selectize"></select>
...
</body>
<script>
const $selectize = $('.selectize').selectize({
options: options,plugins: ['select_on_focus'],...
});
</script>
你看到如何导入插件了吧?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。