将 :checked 伪类添加到嵌入式 mailchimp 表单

Posted

技术标签:

【中文标题】将 :checked 伪类添加到嵌入式 mailchimp 表单【英文标题】:Adding a :checked pseudo class to an embedded mailchimp form 【发布时间】:2018-02-22 01:59:23 【问题描述】:

首先,我并不是一个真正的网络开发者,所以请耐心等待。

我正在尝试在我的网站上嵌入一个 mailchimp 注册表单。我有一组复选框,除非选择了某个单选按钮,否则我希望保持隐藏状态。

    <!-- Begin MailChimp Signup Form -->
<link href="//cdn-images.mailchimp.com/embedcode/classic-10_7.css" rel="stylesheet" type="text/css">
<style type="text/css">

.reveal
    display: none !important;


#radio-2:checked ~ .reveal 
    display: inline;


</style>
<div id="mc_embed_signup">
<form action="n/a" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_blank" novalidate>
    <div id="mc_embed_signup_scroll">

<div class="mc-field-group input-group">
    <strong>Radio Buttons</strong>
    <ul>

<li><input type="radio" id="radio-1" value="Radio 1" name="MMERGE6" id="mce-MMERGE6-0"><label for="mce-MMERGE6-0">Radio 1</label></li>

<li><input type="radio"  id="radio-2" value="Part Time" name="MMERGE6" id="mce-MMERGE6-1"><label for="mce-MMERGE6-1">Part Time</label></li>

</ul>
</div>
<div class="mc-field-group input-group reveal">
    <ul>

<li><input type="checkbox" value="1" name="group[18657][1]" id="mce-group[18657]-18657-0"><label for="mce-group[18657]-18657-0">1</label></li>

<li><input type="checkbox" value="2" name="group[18657][2]" id="mce-group[18657]-18657-1"><label for="mce-group[18657]-18657-1">2</label></li>

<li><input type="checkbox" value="4" name="group[18657][4]" id="mce-group[18657]-18657-2"><label for="mce-group[18657]-18657-2">3</label></li>

<li><input type="checkbox" value="8" name="group[18657][8]" id="mce-group[18657]-18657-3"><label for="mce-group[18657]-18657-3">4</label></li>

</ul>

</div>
    <div id="mce-responses" class="clear">
        <div class="response" id="mce-error-response" style="display:none"></div>
        <div class="response" id="mce-success-response" style="display:none"></div>
    </div>    <!-- real people should not fill this in and expect good things - do not remove this or risk form bot signups-->
    <div style="position: absolute; left: -5000px;" aria-hidden="true"><input type="text" name="b_e7f52c90ee3d55370f2c41bac_8ea8200d35" tabindex="-1" value=""></div>
    <div class="clear"><input type="submit" value="Subscribe" name="subscribe" id="mc-embedded-subscribe" class="button"></div>
    </div>
</form>
</div>
<script type='text/javascript' src='//s3.amazonaws.com/downloads.mailchimp.com/js/mc-validate.js'></script><script type='text/javascript'>(function($) window.fnames = new Array(); window.ftypes = new Array();fnames[0]='EMAIL';ftypes[0]='email';fnames[1]='FNAME';ftypes[1]='text';fnames[2]='LNAME';ftypes[2]='text';fnames[4]='MMERGE4';ftypes[4]='phone';fnames[6]='MMERGE6';ftypes[6]='radio';(jQuery));var $mcj = jQuery.noConflict(true);</script>
<!--End mc_embed_signup-->

我希望此处的复选框集仅在选择第二个单选按钮时显示。

当我编写自己的表单时,我可以得到 :checked 工作,但我似乎无法让它在嵌入式 mailchimp 表单中工作,我不知道为什么。有人有什么想法吗?

【问题讨论】:

【参考方案1】:

这里有几个问题:

    具有!important 规则的样式不能被不包含!important 的其他样式覆盖。您只需将其从您的第一个样式中删除即可解决此问题。

    ~ 同级 CSS 选择器定位元素 with the same parent。因为 radio 和 .reveal 元素不共享同一个父元素,所以它不会显示 .reveal 元素。

    单选按钮不应有两个id 属性。当存在多个时,只设置第二个id

为了使同级选择器起作用,您可以从单选按钮中删除 &lt;ul&gt;&lt;li&gt; 元素,并在它们之前添加换行符,以便它们仍然位于不同的行上。还将.reveal&lt;div&gt; 放在第一个.mc-field-group 中。这将使单选按钮与隐藏元素具有相同的父级。

更新后的代码如下所示:

<!-- Begin MailChimp Signup Form -->
<link href="//cdn-images.mailchimp.com/embedcode/classic-10_7.css" rel="stylesheet" type="text/css">
<style type="text/css">

.reveal 
    display: none;


#mce-MMERGE6-1:checked ~ .reveal 
    display: inline;


</style>
<div id="mc_embed_signup">
    <form action="n/a" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_blank" novalidate>
        <div id="mc_embed_signup_scroll">
            <div class="mc-field-group input-group">
                <strong>Radio Buttons</strong>
                <br>
                <input type="radio" value="Radio 1" name="MMERGE6" id="mce-MMERGE6-0"><label for="mce-MMERGE6-0">Radio 1</label>
                <br>
                <input type="radio" value="Part Time" name="MMERGE6" id="mce-MMERGE6-1"><label for="mce-MMERGE6-1">Part Time</label>
                <div class="reveal">
                    <ul>
                        <li><input type="checkbox" value="1" name="group[18657][1]" id="mce-group[18657]-18657-0"><label for="mce-group[18657]-18657-0">1</label></li>
                        <li><input type="checkbox" value="2" name="group[18657][2]" id="mce-group[18657]-18657-1"><label for="mce-group[18657]-18657-1">2</label></li>
                        <li><input type="checkbox" value="4" name="group[18657][4]" id="mce-group[18657]-18657-2"><label for="mce-group[18657]-18657-2">3</label></li>
                        <li><input type="checkbox" value="8" name="group[18657][8]" id="mce-group[18657]-18657-3"><label for="mce-group[18657]-18657-3">4</label></li>
                    </ul>
                </div>
            </div>
            <div id="mce-responses" class="clear">
                <div class="response" id="mce-error-response" style="display:none"></div>
                <div class="response" id="mce-success-response" style="display:none"></div>
            </div>
            <!-- real people should not fill this in and expect good things - do not remove this or risk form bot signups-->
            <div style="position: absolute; left: -5000px;" aria-hidden="true"><input type="text" name="b_e7f52c90ee3d55370f2c41bac_8ea8200d35" tabindex="-1" value=""></div>
            <div class="clear"><input type="submit" value="Subscribe" name="subscribe" id="mc-embedded-subscribe" class="button"></div>
        </div>
    </form>
</div>
<script type='text/javascript' src='//s3.amazonaws.com/downloads.mailchimp.com/js/mc-validate.js'></script><script type='text/javascript'>(function($) window.fnames = new Array(); window.ftypes = new Array();fnames[0]='EMAIL';ftypes[0]='email';fnames[1]='FNAME';ftypes[1]='text';fnames[2]='LNAME';ftypes[2]='text';fnames[4]='MMERGE4';ftypes[4]='phone';fnames[6]='MMERGE6';ftypes[6]='radio';(jQuery));var $mcj = jQuery.noConflict(true);</script>
<!--End mc_embed_signup-->

【讨论】:

以上是关于将 :checked 伪类添加到嵌入式 mailchimp 表单的主要内容,如果未能解决你的问题,请参考以下文章

伪类checked

12.伪类选择器与伪元素的应用

CSS3:未经检查的伪类

伪元素和伪类

www.xttblog.com伪类选择器:E:checkedE:default和E:indeterminate

伪元素和伪类