为什么我的“最小"和“最大"表格功能不起作用? [英] Why are my 'min' and 'max' form functions not working?

查看:48
本文介绍了为什么我的“最小"和“最大"表格功能不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个自定义表单,以显示在Wordpress Woocommerce结帐页面中,在该页面中,用户必须选择年龄才能进行购买.

I made a custom form to display in Wordpress Woocommerce checkout page, where user must select their age before making the purchase.

这个想法是为了让18岁以下的人无法购买.

The idea is so that people who are under 18 years old would not be able to make a purchase.

当前,代码显示了表单并且已全部设置,但是出于某些原因,我需要将年龄"和最大"功能限制为18至99岁之间插入年龄.我需要什么才能使最小"和最大"功能生效?

Currently, the code displays the form and it is all set, but 'min' and 'max' features, which I would require to limit age insertion between 18 and 99, do not apply for some reason. What do I require in order to make 'min' and 'max' functions to apply?

add_action('woocommerce_after_order_notes', 'custom_checkout_field');

function custom_checkout_field($checkout)
{
    echo '<div id="custom_checkout_field"><h3>' . __('Vanusepiirang') . '</h3>';

    woocommerce_form_field('custom_field_name', array(
        'type'      => 'number',
        'id'        => 'vanusepiirang',
        'name'      => 'vanusepiirang',
        'min'       =>  18,
        'max'       =>  99,
        'required'  =>  1,
        'class'     => array(
            'my-field-class form-row-wide'
        ),
        'label'     => __('Sisestage enda vanus. Teie sünnipäeva küsime kinnituseks, et olete piisavalt vana ostukorvis olevate toodete ostmiseks.') ,
        'placeholder' => __('Teie vanus') ,
    ),

    $checkout->get_value('custom_field_name'));
    echo '</div>';
}

我希望表单上应用最小"和最大"功能,以使数字1-17不能通过.

I expect 'min' and 'max' features to apply on the form, so that numbers 1-17 cannot be passed through.

推荐答案

根据

According to the woocommerce_form_field function definition, max and min attributes must be passed as custom_attribute. Your final code must be modified as below:

add_action('woocommerce_after_order_notes', 'custom_checkout_field');

function custom_checkout_field($checkout)
{
    echo '<div id="custom_checkout_field"><h3>' . __('Vanusepiirang') . '</h3>';

    woocommerce_form_field('custom_field_name', array(
        'type'      => 'number',
        'id'        => 'vanusepiirang',
        'name'      => 'vanusepiirang',
        'custom_attributes' => array(
            'min'       =>  18,
            'max'       =>  99,
        ),
        'required'  =>  1,
        'class'     => array(
            'my-field-class form-row-wide'
        ),
        'label'     => __('Sisestage enda vanus. Teie sünnipäeva küsime kinnituseks, et olete piisavalt vana ostukorvis olevate toodete ostmiseks.') ,
        'placeholder' => __('Teie vanus') ,
    ),

    $checkout->get_value('custom_field_name'));
    echo '</div>';
}

另外,您应该考虑 woocommerce_form_field 函数的第一个参数是字段名称和ID,除非您在第二个参数中分别传递ID参数.这意味着,由于'name'=>"vanusepiirang" 键值生成的输入html将包含两个不同的name属性.我建议删除'name'=>源代码中的"vanusepiirang" 部分.

Also you should consider that, first argument of the woocommerce_form_field function is the field name and ID, unless you pass an ID parameter separately in the second argument. Which means, because of the 'name' => 'vanusepiirang' key-value generated input html will contain two different name attribute. I recommend to remove 'name' => 'vanusepiirang' part from your source code.

这篇关于为什么我的“最小"和“最大"表格功能不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆