如何使用 jQuery 和单选按钮切换两个图像?

发布于 2024-09-05 02:31:14 字数 686 浏览 1 评论 0原文

这并不漂亮,但我想你会明白我想要做的。

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

掩于岁月 2024-09-12 02:31:14
if ($("jpgSel:checked").val() == 1) {

应该阅读

if ($("#jpgSel:checked").val() == 1) {

还是这是一个错字?

另外,您可能想单独使用 $('#jpgSel:checked') ,而不使用 == 1

edit

哦,还有 $("input[@name='jpgSel']").click(function(){ 可能会更好。

if ($("jpgSel:checked").val() == 1) {

should read

if ($("#jpgSel:checked").val() == 1) {

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.

蓝戈者 2024-09-12 02:31:14

您有几个问题:

  • #showjpg 元素是一个 div,您应该有一个 img 元素。
  • 您用于绑定 change 事件的选择器不正确 ($("select")),它应该类似于 $("input[name= jpgSel]")
  • 如果将更改事件绑定到两个单选按钮,则只需通过 this.value 即可获取所选元素的值。

试试这个:

$(document).ready(function() {
  $("input[name=jpgSel]").change(function () {
    if (this.value == "0") {
      $('#showjpg').attr({
        src: 'one.jpg',
        alt: 'one dot jpg'
      });
    } else {
      $('#showjpg').attr({
        src: 'two.jpg',
        alt: 'two dot jpg'
      });
    }
  });
});

此处查看示例

You have several issues:

  • The #showjpg element is a div, you should have there an img element.
  • The selector you are using to bind the change event is incorrect ($("select")), it should be something like $("input[name=jpgSel]")
  • If you bind the change event to both radio buttons, you can get the value of the selected element simply by this.value.

Try this:

$(document).ready(function() {
  $("input[name=jpgSel]").change(function () {
    if (this.value == "0") {
      $('#showjpg').attr({
        src: 'one.jpg',
        alt: 'one dot jpg'
      });
    } else {
      $('#showjpg').attr({
        src: 'two.jpg',
        alt: 'two dot jpg'
      });
    }
  });
});

Check an example here.

蓝海 2024-09-12 02:31:14

jpgSel 是一个名称,必须像这样选择,

$("[name='jpgSel']:checked") 

尽管我认为废弃它并像这样使用 .click() 函数会更容易

$("[name='jpgSel'][value='0']").click(function{/*do something*/});
$("[name='jpgSel'][value='1']").click(function{/*do something else*/});

jpgSel is a name and has to be selected like this

$("[name='jpgSel']:checked") 

although i think it would be easier to scrap that and use the .click() function like so

$("[name='jpgSel'][value='0']").click(function{/*do something*/});
$("[name='jpgSel'][value='1']").click(function{/*do something else*/});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文