【问题标题】:Wordpress contact form 7 image instead of radio buttonWordpress 联系表格 7 图像而不是单选按钮
【发布时间】:2020-06-12 18:29:25
【问题描述】:

正如标题所暗示的,我想使用图像而不是单选按钮,以便用户可以选择图片。我知道这里有一个修改php代码的解决方案,但这似乎已经过时了。我还看到了这个https://jsfiddle.net/La8wQ/10/ 解决方案,如果插件能够以与示例中相同的方式生成 html,它将非常有用。

相反,它在我的页面上看起来像这样:

<span class="wpcf7-list-item first">
<span class="wpcf7-list-item-label">::before Type 1 ::after</span>
<input type="radio" name="type-of-website" value="Type 1" checked="checked">
</span>

这是针对我添加的每种类型的。我知道我可以在标签中扭曲它,但我看不出它在这种情况下会有什么帮助。我想坚持展示示例,因为它使用纯 css。

我想我的问题到底是,我怎样才能让它在我的情况下工作?

【问题讨论】:

    标签: php css wordpress image radio-button


    【解决方案1】:

    欢迎来到 Stack Overflow!

    为此,您需要做一些事情。制作单选按钮,&lt;label&gt;&lt;/label&gt; 直接在它们的下方/之后。这不是默认的 WPCF7 标签。单选按钮需要有一个 id="" 与上面标签的 for="" 部分相同。

    &lt;label&gt; 需要添加一个类(例如 visa),其图像在 CSS 中定义如下:.visa{background-image:url(http://i.imgur.com/lXzJ1eB.png);}

    &lt;label&gt; 也需要这个类:drinkcard-cc

    每个单选按钮都需要添加 wpcf7-special-radio 类。

    对于 2 个单选按钮,示例如下所示:

    HTML

    <form>
        <div>
          <span class="wpcf7-list-item first">
          <input id="special_radio_1" type="radio" class="wpcf7-special-radio" name="type-of-website" value="Type 1"/>
          <label for="special_radio_1" class="drinkcard-cc visa"></label>
          <input id="special_radio_2" type="radio" class="wpcf7-special-radio" name="type-of-website" value="Type 2"/>
          <label for="special_radio_2" class="drinkcard-cc mastercard"></label>
          </span>
        </div>
    </form>
    

    CSS

    .wpcf7-special-radio {
        margin:0;padding:0;
        -webkit-appearance:none;
           -moz-appearance:none;
                appearance:none;
    }
    .visa{background-image:url(http://i.imgur.com/lXzJ1eB.png);}
    .mastercard{background-image:url(http://i.imgur.com/SJbRQF7.png);}
    
    .wpcf7-special-radio:active +.drinkcard-cc{opacity: .9;}
    .wpcf7-special-radio:checked +.drinkcard-cc{
        -webkit-filter: none;
           -moz-filter: none;
                filter: none;
    }
    .drinkcard-cc{
        cursor:pointer;
        background-size:contain;
        background-repeat:no-repeat;
        display:inline-block;
        width:100px;height:70px;
        -webkit-transition: all 100ms ease-in;
           -moz-transition: all 100ms ease-in;
                transition: all 100ms ease-in;
        -webkit-filter: brightness(1.8) grayscale(1) opacity(.7);
           -moz-filter: brightness(1.8) grayscale(1) opacity(.7);
                filter: brightness(1.8) grayscale(1) opacity(.7);
    }
    .drinkcard-cc:hover{
        -webkit-filter: brightness(1.2) grayscale(.5) opacity(.9);
           -moz-filter: brightness(1.2) grayscale(.5) opacity(.9);
                filter: brightness(1.2) grayscale(.5) opacity(.9);
    }
    

    这是一个有效的小提琴https://jsfiddle.net/Snuwerd/9r1dz0uk/

    更新

    更正,标签需要在输入之后/下方,如示例代码中所示。

    更新

    没有 JS 是不可能的,因为在 WPCF7 中不能给单选元素分配单独的 ID,所以这里是 JS 的解决方案:

    使用这个 CSS:

    .wpcf7-special-radio-container .wpcf7-list-item-label {
     display: none;
    }
    .wpcf7-special-radio {
        margin:0;padding:0;
        -webkit-appearance:none;
           -moz-appearance:none;
                appearance:none;
    }
    .special_radio_card_0 { background-image:url(http://i.imgur.com/lXzJ1eB.png); }
    .special_radio_card_1 { background-image:url(http://i.imgur.com/SJbRQF7.png); }
    .special_radio_card_2 { background-image:url(http://i.imgur.com/lXzJ1eB.png); }
    .special_radio_card_3 { background-image:url(http://i.imgur.com/SJbRQF7.png); }
    .special_radio_card_4 { background-image:url(http://i.imgur.com/lXzJ1eB.png); }
    .special_radio_card_5 { background-image:url(http://i.imgur.com/SJbRQF7.png); }
    
    .wpcf7-special-radio:active +.drinkcard-cc{opacity: .9;}
    .wpcf7-special-radio:checked +.drinkcard-cc{
        -webkit-filter: none;
           -moz-filter: none;
                filter: none;
    }
    .drinkcard-cc{
        cursor:pointer;
        background-size:contain;
        background-repeat:no-repeat;
        display:inline-block;
        width:100px;height:70px;
        -webkit-transition: all 100ms ease-in;
           -moz-transition: all 100ms ease-in;
                transition: all 100ms ease-in;
        -webkit-filter: brightness(1.8) grayscale(1) opacity(.7);
           -moz-filter: brightness(1.8) grayscale(1) opacity(.7);
                filter: brightness(1.8) grayscale(1) opacity(.7);
    }
    .drinkcard-cc:hover{
        -webkit-filter: brightness(1.2) grayscale(.5) opacity(.9);
           -moz-filter: brightness(1.2) grayscale(.5) opacity(.9);
                filter: brightness(1.2) grayscale(.5) opacity(.9);
    }
    

    确保不要在&lt;script&gt;&lt;/script&gt; 标记内添加任何白(空)行,否则它会中断,因为 WPCF7 会在代码中放置段落(&lt;p&gt;)。将此粘贴到您的表单中:

    <div class="row">
        <div class="col-md-6"> <label> Your Name * [text* your-name] </label> </div>
        <div class="col-md-6"> <label> Your Email * [email* your-email] </label> </div>
    </div>
    <label> Subject [text your-subject] </label> <label> Your Message [textarea your-message] </label>
    
    [radio type-of-website class:wpcf7-special-radio-container default:1 "Type 1" "Type 2"]
    [submit "Send"]
    
    <script>
    jQuery(document).ready(function($) {    
        $('.wpcf7-special-radio-container input').addClass('wpcf7-special-radio');
        $('.wpcf7-special-radio').each(function (index, el) {
            var label = $('<label>').attr('for', 'special_radio_'+index).addClass('drinkcard-cc').addClass('special_radio_card_'+index);
            $(this).attr('id', 'special_radio_'+index);
            $(this).after(label);
        }); 
    });
    </script>
    

    您可以像这样在收音机中添加新选项

    2 个选项: [radio type-of-website class:wpcf7-special-radio-container default:1 "Type 1" "Type 2"]

    4 个选项:[radio type-of-website class:wpcf7-special-radio-container default:1 "Type 1" "Type 2" "Type 3" "Type 4"]

    它们将与这些行的 CSS 代码中选择的图像按顺序映射:

    .special_radio_card_0 { background-image:url(http://i.imgur.com/lXzJ1eB.png); }
    .special_radio_card_1 { background-image:url(http://i.imgur.com/SJbRQF7.png); }
    .special_radio_card_2 { background-image:url(http://i.imgur.com/lXzJ1eB.png); }
    .special_radio_card_3 { background-image:url(http://i.imgur.com/SJbRQF7.png); }
    .special_radio_card_4 { background-image:url(http://i.imgur.com/lXzJ1eB.png); }
    .special_radio_card_5 { background-image:url(http://i.imgur.com/SJbRQF7.png); }
    

    【讨论】:

    • 感谢您的详细解释。我现在唯一想不通的是如何使用插件插入标签标签。因为谷歌搜索这个问题会导致其他人感到困惑。我厌倦了在插件中手动添加它,但这会破坏一切。我也不能将 id 添加到每个选项中,只能添加到完整表单中。
    • 只需将html直接添加到表单中即可。您可以在 CF7 表单中使用 HTML。可以发一下表格代码吗?也可能是表单在页面上生成的 HTML 代码。
    • &lt;div class="row"&gt; &lt;div class="col-md-6"&gt; &lt;label&gt; Your Name * [text* your-name] &lt;/label&gt; &lt;/div&gt; &lt;div class="col-md-6"&gt; &lt;label&gt; Your Email * [email* your-email] &lt;/label&gt; &lt;/div&gt; &lt;/div&gt; &lt;label&gt; Subject [text your-subject] &lt;/label&gt; &lt;label&gt; Your Message [textarea your-message] &lt;/label&gt; [radio type-of-website class:cc-selector default:1 "Type 1" "Type 2"] [submit "Send"] 这是 CP7 的样子,我无法发布生成的 html,因为评论太长了。但它基本上是我原始代码的样子。
    • 我做了一些额外的研究,不得不让你失望。因为您无法使用 CF7 为单选选项分配单独的 ID,所以没有 JS 就无法实现您想要的。我可以编写一个脚本来实现它,您只需将其粘贴到 CF7 表单的底部即可。
    • 是的,我正要说同样的话。其实很安静很悲伤。如果您能帮我处理 js,我将不胜感激。
    猜你喜欢
    • 2016-06-30
    • 2015-01-20
    • 1970-01-01
    • 2016-07-29
    • 2018-01-26
    • 2019-03-31
    • 1970-01-01
    • 1970-01-01
    • 2020-03-30
    相关资源
    最近更新 更多