1

我想禁用 woocommerce 结帐页面上的 select2 下拉菜单。我希望用户不能从结帐页面的下拉菜单中更改国家/地区

我不想隐藏字段(国家),但只想禁用下拉菜单

这就是 html 查找下拉菜单的方式

<div class="select2-drop select2-display-none select2-with-searchbox select2-drop-active" style="left: 92.5px; width: 556px; top: 668px; bottom: auto; display: block;" id="select2-drop">   

    <div class="select2-search">      
        <label for="s2id_autogen1_search" class="select2-offscreen">Country *</label>       
        <input type="text" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" class="select2-input" role="combobox" aria-expanded="true" aria-autocomplete="list" aria-owns="select2-results-1" id="s2id_autogen1_search" placeholder="" aria-activedescendant="select2-result-label-7">   
    </div>   
    <ul class="select2-results" role="listbox" id="select2-results-1">
        <li class="select2-results-dept-0 select2-result select2-result-selectable select2-highlighted" role="presentation">
            <div class="select2-result-label" id="select2-result-label-7" role="option">
            <span class="select2-match">
            </span>
            India
            </div>
        </li>
        <li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation">
            <div class="select2-result-label" id="select2-result-label-8" role="option">
            <span class="select2-match"></span>
            United Kingdom (UK)
            </div>
        </li>
    </ul>
</div>

请帮忙

4

3 回答 3

2

你可以这样做......

function rei_woocommerce_form_field_args($args, $key, $value) {

    if ($key == 'billing_country') {
        $args['custom_attributes'] = array('disabled'=>'disabled');

    }
    return $args;
}
add_filter('woocommerce_form_field_args', 'rei_woocommerce_form_field_args', 10, 3);

将其添加到您的functions.php...

我希望您意识到禁用的表单字段不会包含在 POST / GET 数据中。

如果你真的需要这个,我们可以为选择添加具有相同名称和值的额外隐藏输入..现在所有在一起,应该是这样的..

function rei_woocommerce_form_field_args($args, $key, $value) {

    if (($key == 'billing_country') && is_checkout()) {
        $args['custom_attributes'] = array('disabled'=>'disabled');
    }
    return $args;
}
add_filter('woocommerce_form_field_args', 'rei_woocommerce_form_field_args', 10, 3);

function rei_woocommerce_form_field_country($field, $key, $args, $value){

    if (($key == 'billing_country') && is_checkout()) {
        $field .= '<input type="hidden" name="' . esc_attr( $key ) . '" value="' . esc_attr( $value ) . '" />';
    }
    return $field;
}

add_filter('woocommerce_form_field_country','rei_woocommerce_form_field_country', 10, 4);

另一个注意事项是,即使您可以禁用选择,任何有知识的人都可以轻松删除浏览器中的禁用属性。

于 2016-01-09T02:52:58.603 回答
0

您可以使用的两种解决方法

  1. 在 country 字段上方添加一个透明层,并为其赋予更高的 z-index。
  2. 使用 JQuery 并返回 false;当 onClick 和 onSelect 事件被调用时。
于 2016-01-09T01:29:11.577 回答
-3

在输入中添加disabled属性,如下所示。

<input type="text" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" class="select2-input" role="combobox" aria-expanded="true" aria-autocomplete="list" aria-owns="select2-results-1" id="s2id_autogen1_search" placeholder="" aria-activedescendant="select2-result-label-7" disabled>
于 2016-01-09T01:14:28.107 回答