如何使用 jQuery 和单选按钮切换两个图像?
这并不漂亮,但我想你会明白我想要做的。
<label><input name="jpgSel" type="radio" value="0">jpg 1</label><br />
<label><input name="jpgSel" type="radio" value="1">jpg 2</label><br />
<div id="showjpg"></div>
和
$(document).ready(function() {
$("select").change(function () {
if ($("jpgSel:checked").val() == 1) {
$('#showjpg').attr({
src: 'one.jpg',
alt: 'one dot jpg'
});
}
else {
$('#showjpg').attr({
src: 'two.jpg',
alt: 'two dot jpg'
});
}
});
This isn't pretty, but I think you'll get what I'm trying to do.
<label><input name="jpgSel" type="radio" value="0">jpg 1</label><br />
<label><input name="jpgSel" type="radio" value="1">jpg 2</label><br />
<div id="showjpg"></div>
and
$(document).ready(function() {
$("select").change(function () {
if ($("jpgSel:checked").val() == 1) {
$('#showjpg').attr({
src: 'one.jpg',
alt: 'one dot jpg'
});
}
else {
$('#showjpg').attr({
src: 'two.jpg',
alt: 'two dot jpg'
});
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
应该阅读
还是这是一个错字?
另外,您可能想单独使用
$('#jpgSel:checked')
,而不使用== 1
edit
哦,还有
$("input[@name='jpgSel']").click(function(){
可能会更好。should read
or was that a typo?
Also you may want to use
$('#jpgSel:checked')
on its own w/out== 1
edit
oh and
$("input[@name='jpgSel']").click(function(){
might be better.您有几个问题:
#showjpg
元素是一个div
,您应该有一个img
元素。change
事件的选择器不正确 ($("select")
),它应该类似于$("input[name= jpgSel]")
this.value
即可获取所选元素的值。试试这个:
在此处查看示例。
You have several issues:
#showjpg
element is adiv
, you should have there animg
element.change
event is incorrect ($("select")
), it should be something like$("input[name=jpgSel]")
this.value
.Try this:
Check an example here.
jpgSel 是一个名称,必须像这样选择,
尽管我认为废弃它并像这样使用 .click() 函数会更容易
jpgSel is a name and has to be selected like this
although i think it would be easier to scrap that and use the .click() function like so