如何使HTML选择选项可搜索

时间:2022-01-15 作者:My display name

有什么简单的方法可以修改源代码并使选择选项在产品页面中搜索属性?就像我打字的时候,它暗示了价值。

<div id="product_attributes" class="panel wc-metaboxes-wrapper hidden">
<div class="toolbar toolbar-top">
    <span class="expand-close">
        <a href="#" class="expand_all"><?php esc_html_e( \'Expand\', \'woocommerce\' ); ?></a> / <a href="#" class="close_all"><?php esc_html_e( \'Close\', \'woocommerce\' ); ?></a>
    </span>
    <select name="attribute_taxonomy" class="attribute_taxonomy">
        <option value=""><?php esc_html_e( \'Custom product attribute\', \'woocommerce\' ); ?></option>
        <?php
        global $wc_product_attributes;

        // Array of defined attribute taxonomies.
        $attribute_taxonomies = wc_get_attribute_taxonomies();

        if ( ! empty( $attribute_taxonomies ) ) {
            foreach ( $attribute_taxonomies as $tax ) {
                $attribute_taxonomy_name = wc_attribute_taxonomy_name( $tax->attribute_name );
                $label                   = $tax->attribute_label ? $tax->attribute_label : $tax->attribute_name;
                echo \'<option value="\' . esc_attr( $attribute_taxonomy_name ) . \'">\' . esc_html( $label ) . \'</option>\';
            }
        }
        ?>
    </select>
    <button type="button" class="button add_attribute"><?php esc_html_e( \'Add\', \'woocommerce\' ); ?></button>
</div>

1 个回复
最合适的回答,由SO网友:My display name 整理而成

答案是:

<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/select2.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/select2.min.js"></script>
<div id="product_attributes" class="panel wc-metaboxes-wrapper hidden">
<div class="toolbar toolbar-top">
<span class="expand-close">
<a href="#" class="expand_all"><?php esc_html_e( \'Expand\', \'woocommerce\' ); ?></a> / <a href="#" class="close_all"><?php esc_html_e( \'Close\', \'woocommerce\' ); ?></a>
</span>
<select name="attribute_taxonomy" class="attribute_taxonomy js-ao-select-options">
<option value=""><?php esc_html_e( \'Custom product attribute\', \'woocommerce\' ); ?></option>
<?php
global $wc_product_attributes;
$attribute_taxonomies = wc_get_attribute_taxonomies();
if ( ! empty( $attribute_taxonomies ) ) {
foreach ( $attribute_taxonomies as $tax ) {
$attribute_taxonomy_name = wc_attribute_taxonomy_name( $tax->attribute_name );
$label                   = $tax->attribute_label ? $tax->attribute_label : $tax->attribute_name;
echo \'<option value="\' . esc_attr( $attribute_taxonomy_name ) . \'">\' . esc_html( $label ) . \'</option>\';
}} ?>
</select>
<script>
jQuery(document).ready(function($){
$(\'.js-ao-select-options\').select2();
});
</script>
<button type="button" class="button add_attribute"><?php esc_html_e( \'Add\', \'woocommerce\' ); ?></button>
从Select区域提交后,width的值变小了,所以我直接修改了CSS(Plugin/woocomece/admin.CSS)以获得更好的效果,但通常不应该像我那样直接修改。该部分负责选择区域的宽度:

.select2-container .select2-selection--single .select2-selection__rendered{line-height:40px;padding-right:24px;width:500px;}