如何使 woocommerce select 字段中的第一个选项无法 select?

How to make the first option in a woocommerce select field unselectable?

我在 wordpress 中使用 php 函数将自定义选择字段添加到我的 woocommerce 结帐。 我想让第一个选项(名为空白)不可选择。我怎样才能做到这一点?


     woocommerce_form_field( 'cstm_leeftijd'.$i, array(
            'type'          => 'select',
            'class'         => array('my-field-class form-row-first'),
            'label'         => __('Leeftijd bij start Summer Camp'),
            'required' => true,
            'options'       => array(
                 'blank'     => __( 'Selecteer', 'njengah' ),
                 '4'         => __( '4', 'njengah' ),
                 '5'         => __( '5', 'njengah' ),
                 '6'         => __( '6', 'njengah' ),
                 '7'         => __( '7', 'njengah' ),
                 '8'         => __( '8', 'njengah' ),
                 '9'         => __( '9', 'njengah' ),
                 '10'        => __( '10', 'njengah' ),
                 '11'        => __( '11', 'njengah' )
        )), $checkout->get_value( 'cstm_leeftijd'.$i ));

您可以像这样使用占位符变量:

 woocommerce_form_field( 'cstm_leeftijd'.$i, array(
            'type'          => 'select',
            'class'         => array('my-field-class form-row-first'),
            'label'         => __('Leeftijd bij start Summer Camp'),
            'required' => true,
            'placeholder'   => __( 'Selecteer', 'njengah' ),
            'options'       => array(
                 '4'         => __( '4', 'njengah' ),
                 '5'         => __( '5', 'njengah' ),
                 '6'         => __( '6', 'njengah' ),
                 '7'         => __( '7', 'njengah' ),
                 '8'         => __( '8', 'njengah' ),
                 '9'         => __( '9', 'njengah' ),
                 '10'        => __( '10', 'njengah' ),
                 '11'        => __( '11', 'njengah' )
        )), $checkout->get_value( 'cstm_leeftijd'.$i ));

disabled 属性传递给第一个选项就可以了。 您可以检查 woocommerce_form_field() 函数机制以检查它支持以何种方式传递禁用属性。

您甚至可以通过 jquery 完成此操作。通过您的自定义 class my-field-class

$(".my-field-class select option:first").attr("disabled", "true");