jquery输入未检查图像路径更改

时间:2017-08-14 09:07:13

标签: javascript jquery input

我不知道它是如何运作的,所以我希望你能帮助我。 如果输入字段未选中,我想更改图片的图像路径。 如果检查,它将路径更改为彩色图片,这对我来说很好,但如果我选择另一个图像,之前的图像应该变回黑白图像。

提前致谢!

这是html:



$('input[name=an_paymentOptionsControl]').on('change', function() {
  $(this).siblings('img').attr('src', 'pics/payment-large/' + this.getAttribute('value') + '.png');
  console.log(this.getAttribute('value'));
});

$('input[name=an_paymentOptionsControl]').not(':checked', function() {
  $('img').attr('src', 'pics/payment-large/sw' + this.getAttribute('value') + '.png');
  console.log('nix');
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="anm-payment-layout-methods">
  <label>
          <input class="anm-pay-method" type="radio" value="acc" name="an_paymentOptionsControl" />
          <img class="paymentLabel" src="pics/payment-large/swacc.png">
        </label>
  <label>
          <input class="anm-pay-method" type="radio" value="paypal" name="an_paymentOptionsControl" />
          <img src="pics/payment-large/swpaypal.png">
        </label>
  <label>
            <input class="anm-pay-method" type="radio" value="sue" name="an_paymentOptionsControl" />
            <img src="pics/payment-large/swsue.png">
        </label>
  <label>
            <input class="anm-pay-method" type="radio" value="giropay" name="an_paymentOptionsControl" />
            <img src="pics/payment-large/swgiropay.png">
        </label>
  <label>
            <input class="anm-pay-method" type="radio" value="prepay" name="an_paymentOptionsControl" />
            <img src="pics/payment-large/swprepay.png">
        </label>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

更新change事件处理程序中的图像路径。

$('input[name=an_paymentOptionsControl]').on('change', function () {
    $(this).siblings('img').attr('src', 'pics/payment-large/' + this.value + '.png');

    //Reset images
    $('input[name=an_paymentOptionsControl]:not(:checked)').each(function () {
        $(this).siblings('img').attr('src', 'pics/payment-large/sw' + this.value + '.png');
    });
});