如何使用单选按钮、3 个图像和 3 个状态进行图像交换?
我有 3 个图像,当单击 3 个单选按钮中的每一个时,需要切换到打开状态 - 并在未选中它们时返回。
足够简单: 与单选按钮关联的值是图像的 id 和图像的名称(减去 state )。
我希望在单击按钮时将其与图像 src
绑定。
我该如何让它发挥作用?
$(document).ready(function() {
$(".challengeradio").change(function(){
if(this.checked){
$("#" + this.value +".pic").attr("src").replace("_off", "_on");
}
});
});
~~~以及所请求的XHTML~
<div id="chooseChallenge">
<h2> Choose a Challenge </h2>
<div class="badge" id="weightLossChallenge">
<div class="challengelabel" id="chall_weightloss">
<input type="radio" value="chall_weightloss" name="challenge" class="challengeradio">
<strong> the big <br>Thang</strong>
</div>
<img border="0" src="http://www.yoozit.net/zone/images/chall_weightloss_on.png" id="chall_weightloss" class="pic"> </div>
<div class="badge" id="activityChallenge">
<div class="challengelabel" id="chall_activity">
<input type="radio" value="chall_activity" name="challenge" class="challengeradio">
<strong> Activity Tracker<br>Thang</strong>
</div> <img border="0" src="http://www.yoozit.net/zone/images/chall_activity_off.png" id="chall_activity" class="pic"></div>
<div class="badge" id="basicChallenge">
<div class="challengelabel" id="chall_basic">
<input type="radio" value="chall_basic" name="challenge" class="challengeradio">
<strong>Basic Standard <br>Thang</strong>
</div>
<img border="0" src="http://www.yoozit.net/zone/images/chall_basic_off.png" id="chall_basic" class="pic"></div>
</div>
I have 3 images that need to switch to on state
when each of 3 radio buttons are clicked - and back when they are not checked.
Simple enough:
The value associated with the radio buttons is the id of the image and the name of the image ( minus the state ).
Which I hope to tie to the image src
when the button is clicked.
How do I get this to work?
$(document).ready(function() {
$(".challengeradio").change(function(){
if(this.checked){
$("#" + this.value +".pic").attr("src").replace("_off", "_on");
}
});
});
~~~ and the requested XHTML~
<div id="chooseChallenge">
<h2> Choose a Challenge </h2>
<div class="badge" id="weightLossChallenge">
<div class="challengelabel" id="chall_weightloss">
<input type="radio" value="chall_weightloss" name="challenge" class="challengeradio">
<strong> the big <br>Thang</strong>
</div>
<img border="0" src="http://www.yoozit.net/zone/images/chall_weightloss_on.png" id="chall_weightloss" class="pic"> </div>
<div class="badge" id="activityChallenge">
<div class="challengelabel" id="chall_activity">
<input type="radio" value="chall_activity" name="challenge" class="challengeradio">
<strong> Activity Tracker<br>Thang</strong>
</div> <img border="0" src="http://www.yoozit.net/zone/images/chall_activity_off.png" id="chall_activity" class="pic"></div>
<div class="badge" id="basicChallenge">
<div class="challengelabel" id="chall_basic">
<input type="radio" value="chall_basic" name="challenge" class="challengeradio">
<strong>Basic Standard <br>Thang</strong>
</div>
<img border="0" src="http://www.yoozit.net/zone/images/chall_basic_off.png" id="chall_basic" class="pic"></div>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用 $(this).checked, $(this).value
Try using $(this).checked, $(this).value
尝试
有两个名为
favourite_off.png
和favourite_on.png
的图像演示
try
there are two image with name
favourite_off.png
andfavourite_on.png
DEMO
完毕!
有点 hacky,但可以完成工作:
http://jsfiddle.net/2Sy2h/26/
代码:
Done!
Bit hacky but does the job :
http://jsfiddle.net/2Sy2h/26/
The code :