【问题标题】:How to select value on focus for a selectize?如何选择焦点值以进行选择?
【发布时间】:2021-06-02 15:24:04
【问题描述】:

我想为selectize 选择焦点值。

此时,我可以单击并删除该值,但我希望 click(focus) 应该选择 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(); } );

【问题讨论】:

    标签: javascript php jquery selectize.js


    【解决方案1】:

    经过三个多小时的研究,我找到了最相关的答案。

    我需要将 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>
    

    你明白如何导入插件了吧?

    【讨论】:

      猜你喜欢
      • 2017-06-03
      • 1970-01-01
      • 1970-01-01
      • 2016-07-18
      • 2020-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多