【问题标题】:Add specific attribute and values to input and label html tags in Woocommerce在 Woocommerce 中添加特定属性和值以输入和标记 html 标签
【发布时间】:2019-01-21 00:57:57
【问题描述】:

我正在尝试添加样式,具体取决于是否单击了复选框。此代码缺少输入和标签的“id”和“for”(第 11 行)。添加数字生成的逻辑决定。如何正确操作?

foreach ($choices as $choice) {
    $attr = '';
    $key_val = explode("|", $choice);
    /* It has to be two items ( Value => Label ), otherwise don't proceed */
    if (count($key_val) == 2) {
        if (in_array(trim($key_val[0]), $defaults)) {
            $attr = 'checked';
        }
        /* For admin field, we don't need <li></li> wrapper */
        $html .= (($_ptype != "wccaf") ? '<li>' : '') . '<input type="checkbox" data-has_field_rules="'.$has_field_rules.'" data-is_pricing_rules="'.$_is_pricing_rules.'" class="' . $_ptype . '-field ' . $_class . '" name="' . esc_attr($_meta["name"] . $_index) . '[]" value="' . esc_attr(trim($key_val[0])) . '" ' . $attr . ' ' . $_ptype . '-type="checkbox" ' . $_ptype . '-pattern="mandatory" ' . $_ptype . '-mandatory="' . $_meta["required"] . '" ' . $_readonly . ' /><label class="wcff-option-wrapper-label">' . esc_attr(trim($key_val[1])) . '</label>' . (($_ptype != "wccaf") ? '</li>' : '');
    }
}

【问题讨论】:

    标签: php html wordpress woocommerce attributes


    【解决方案1】:

    更新

    尝试以下(未经测试),这将向&lt;label&gt; 添加一个for 属性+ 值,向&lt;input&gt; 添加一个id 属性+ 值:

    foreach ($choices as $choice) {
        $attr = '';
        $key_val = explode("|", $choice);
        /* It has to be two items ( Value => Label ), otherwise don't proceed */
        if (count($key_val) == 2) {
            if (in_array(trim($key_val[0]), $defaults)) {
                $attr = 'checked';
            }
            $sprintf = sprintf( '<input type="checkbox" %s %s %s %s %s %s %s /><label %s class="wcff-option-wrapper-label">%s</label>',
                'id="' . esc_attr($_meta["name"] . $_index) . '"',
                'data-has_field_rules="'.$has_field_rules.'"',
                'data-is_pricing_rules="'.$_is_pricing_rules.'"',
                'class="' . $_ptype . '-field ' . $_class . '"',
                'name="' . esc_attr($_meta["name"] . $_index) . '[]"',
                'value="' . esc_attr(trim($key_val[0])) . '"',
                $attr . ' ' . $_ptype . '-type="checkbox" ' . $_ptype . '-pattern="mandatory' . $_meta["required"] . '" ' . $_readonly,
                'for="' . esc_attr($_meta["name"] . $_index) . '"',
            esc_attr(trim($key_val[1]) ) );
    
            $html .= $_ptype != "wccaf" ? '<li>'.$sprintf.'</li>' : $sprintf;
        }
    }
    

    我已将代码嵌入到 sprintf() 函数中,使其更具可读性、功能性和易于调整。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-30
      • 1970-01-01
      相关资源
      最近更新 更多