简体   繁体   English

如何使用jQuery将单选按钮替换为图像?

[英]How can I replace radio buttons with images using jQuery?

I have some mark-up that looks like this, it's from an E-commerce CMS. 我有一些像这样的标记,它来自一个电子商务CMS。

<input type="radio" name="id[9]" value="70" id="attrib-9-70"class="threads-opts" />
<label class="attribsRadioButton" for="attrib-9-70">SL01<br />
<img src="images/attributes/SL01_thumb.jpg" alt="" width="100" height="100" /></label>

<input type="radio" name="id[9]" value="71" id="attrib-9-71"class="threads-opts" />
<label class="attribsRadioButton" for="attrib-9-71">SL02<br />
<img src="images/attributes/SL02_thumb.jpg" alt="" width="100" height="100" /></label>

etc etc

what I'd like to do is, have the user be able to click the image(the one in the label immediately following) instead of the radio button, and hide the radio button that is currently there, or have it be replaced with the image. 我想做的是,让用户能够单击图像(紧随其后的标签中的那个)而不是单选按钮,并隐藏当前存在的单选按钮,或者将其替换为图片。

could that be done with jQuery? 可以用jQuery完成吗? or, maybe a better questions is how could you send form data like that with images instead of radio buttons? 或者,也许更好的问题是如何发送带有图像而不是单选按钮的表单数据?

Wrap your radio button within your label like this 这样将单选按钮包裹在标签中

<label class="attribsRadioButton" for="attrib-9-70">
    <input type="radio" name="id[9]" value="70" id="attrib-9-70"class="threads-opts" />
    <img src="images/attributes/SL01_thumb.jpg" alt="" width="100" height="100" />
</label>

The answer by sfletche is correct, though you need not wrap the radio button inside the label, it just has to be in the same form. sfletche的答案是正确的,尽管您不必将单选按钮包装在标签内,它必须采用相同的形式。

Also if you want to hide the radio button use the display:none or visibility:hidden style. 另外,如果要隐藏单选按钮,请使用display:none或visible:hidden样式。

<input type="radio" name="id[9]" value="70" id="attrib-9-70" class="threads-opts" />
<label class="attribsRadioButton" for="attrib-9-70">
    <img src="images/attributes/SL01_thumb.jpg" alt="" width="100" height="100" />
</label>

In your css use: 在您的CSS中使用:

.threads-opts{
    display:none;
    visibility:hidden;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM