更改单选按钮上的背景图像已选中
Change background-image on radio button checked
我目前正在使用重力表单和图像插件在 Wordpress 中创建一个表单,它允许我插入图像而不是单选按钮。但是,我希望在选中单选按钮时更改图像。我的问题是我不知道如何处理这个问题,以及如何在 HTML?
中直接设置背景图像的样式
<div class='ginput_container ginput_container_radio'>
<ul class='gfield_radio' id='input_1_2'>
<li class='gchoice_1_2_0'><input name='input_2' type='radio' value='' id='choice_1_2_0' tabindex='1' /><label for='choice_1_2_0' id='label_1_2_0'><span class="image-choices-choice-image-wrap" style="background-image:url(http://image1.com)"><img src="http://image1.com" alt="" class="image-choices-choice-image" /></span><span class="image-choices-choice-text"></span></label></li>
<li class='gchoice_1_2_1'><input name='input_2' type='radio' value='' id='choice_1_2_1' tabindex='2' /><label for='choice_1_2_1' id='label_1_2_1'><span class="image-choices-choice-image-wrap" style="background-image:url(http://image2.com)"><img src="http://image2.com" alt="" class="image-choices-choice-image" /></span><span class="image-choices-choice-text"></span></label></li>
<li class='gchoice_1_2_2'><input name='input_2' type='radio' value='' id='choice_1_2_2' tabindex='3' /><label for='choice_1_2_2' id='label_1_2_2'><span class="image-choices-choice-image-wrap" style="background-image:url(http://image3.com)"><img src="http://image3.com" alt="" class="image-choices-choice-image" /></span><span class="image-choices-choice-text"></span></label></li>
</ul>
</div>
对于解决此问题的任何帮助,我将不胜感激。
如果您需要更多信息,请告诉我,谢谢
通用形式是:
input + label { /* css for unchecked state */ }
input:checked + label { /* css for checked state */ }
input { display: none; }
input {
display: none;
}
input + label { background-color: red; }
input:checked + label { background-color: black; color: white ; }
<input id=option1 type=radio name=dd checked><label for=option1>select</label>
<input id=option2 type=radio name=dd><label for=option2>select 2</label>
您可以更具体地说明要更改哪个图像以及要从哪个单选按钮触发事件。但我还是会试一试。
<input name='input_2' type='radio' value='' id='choice_1_2_0' tabindex='1' />
<div class="image-choices-choice-image-wrap" style="background-image:url(https://cdn.kimkim.com/files/a/content_articles/featured_photos/d1ea2d6f6d1aa470f661fa661bd0e2fb14fd2d2c/medium-886981758498052b0532dc3a96b98adf.jpg);height:200px;width:400px">
<script>
$('#choice_1_2_0').click(function(){
// If radiobuton is checked
if ($(this).is(':checked'))
{
// Change background image
$('.image-choices-choice-image-wrap').css("background-image", "url(https://cdn.kimkim.com/files/a/content_articles/featured_photos/7a4f69e48562c9adbee4f090033454a8ab23c135/medium-e5cbb5b7a25c93f53245e256729efff8.jpg)");
}
});
</script>
如果您还没有添加jQuery,请不要忘记添加
我最终使用了以下指南:
https://css-tricks.com/override-inline-styles-with-css/
该插件在选中单选按钮时添加了 class .image-choices-choice-selected。因此,我使用以下代码来解决问题:
.image-choices-choice-selected #label_1_2_2 span[style] {
background-image:url(http://newimage1.com) !important;
}
感谢您的建议,我想如果没有添加这个 class,我可以使用 jQuery。
我目前正在使用重力表单和图像插件在 Wordpress 中创建一个表单,它允许我插入图像而不是单选按钮。但是,我希望在选中单选按钮时更改图像。我的问题是我不知道如何处理这个问题,以及如何在 HTML?
中直接设置背景图像的样式 <div class='ginput_container ginput_container_radio'>
<ul class='gfield_radio' id='input_1_2'>
<li class='gchoice_1_2_0'><input name='input_2' type='radio' value='' id='choice_1_2_0' tabindex='1' /><label for='choice_1_2_0' id='label_1_2_0'><span class="image-choices-choice-image-wrap" style="background-image:url(http://image1.com)"><img src="http://image1.com" alt="" class="image-choices-choice-image" /></span><span class="image-choices-choice-text"></span></label></li>
<li class='gchoice_1_2_1'><input name='input_2' type='radio' value='' id='choice_1_2_1' tabindex='2' /><label for='choice_1_2_1' id='label_1_2_1'><span class="image-choices-choice-image-wrap" style="background-image:url(http://image2.com)"><img src="http://image2.com" alt="" class="image-choices-choice-image" /></span><span class="image-choices-choice-text"></span></label></li>
<li class='gchoice_1_2_2'><input name='input_2' type='radio' value='' id='choice_1_2_2' tabindex='3' /><label for='choice_1_2_2' id='label_1_2_2'><span class="image-choices-choice-image-wrap" style="background-image:url(http://image3.com)"><img src="http://image3.com" alt="" class="image-choices-choice-image" /></span><span class="image-choices-choice-text"></span></label></li>
</ul>
</div>
对于解决此问题的任何帮助,我将不胜感激。
如果您需要更多信息,请告诉我,谢谢
通用形式是:
input + label { /* css for unchecked state */ }
input:checked + label { /* css for checked state */ }
input { display: none; }
input {
display: none;
}
input + label { background-color: red; }
input:checked + label { background-color: black; color: white ; }
<input id=option1 type=radio name=dd checked><label for=option1>select</label>
<input id=option2 type=radio name=dd><label for=option2>select 2</label>
您可以更具体地说明要更改哪个图像以及要从哪个单选按钮触发事件。但我还是会试一试。
<input name='input_2' type='radio' value='' id='choice_1_2_0' tabindex='1' />
<div class="image-choices-choice-image-wrap" style="background-image:url(https://cdn.kimkim.com/files/a/content_articles/featured_photos/d1ea2d6f6d1aa470f661fa661bd0e2fb14fd2d2c/medium-886981758498052b0532dc3a96b98adf.jpg);height:200px;width:400px">
<script>
$('#choice_1_2_0').click(function(){
// If radiobuton is checked
if ($(this).is(':checked'))
{
// Change background image
$('.image-choices-choice-image-wrap').css("background-image", "url(https://cdn.kimkim.com/files/a/content_articles/featured_photos/7a4f69e48562c9adbee4f090033454a8ab23c135/medium-e5cbb5b7a25c93f53245e256729efff8.jpg)");
}
});
</script>
如果您还没有添加jQuery,请不要忘记添加
我最终使用了以下指南: https://css-tricks.com/override-inline-styles-with-css/
该插件在选中单选按钮时添加了 class .image-choices-choice-selected。因此,我使用以下代码来解决问题:
.image-choices-choice-selected #label_1_2_2 span[style] {
background-image:url(http://newimage1.com) !important;
}
感谢您的建议,我想如果没有添加这个 class,我可以使用 jQuery。