简体   繁体   English

在jQuery中遍历兄弟姐妹的孩子

[英]Traversing children of siblings in jquery

I want to change the background image of a radio button when it is checked. 我想在选中时更改单选按钮的背景图像。 I have hidden the radio button and wrapped it inside a label>span>img . 我已经隐藏了单选按钮,并将其包装在label>span>img I want to change the src of the image when the radio is checked. 我想在检查收音机时更改图像的src。

<div class = " monthly-radio-div padding-lr-zero radio-inline">
   <input type="radio"  name="inv_type" id="monthly" value = "monthly" >
   <label for="monthly">
      <span class="radio">
         <img src="icons/r_dis.png">
      </span>
   </label>
</div>

I want to change the src of the image. 我想更改图像的src。 I tried few ways which didn't work 我尝试了几种无效的方法

$('#fund_sel_form input').on('change', function() {
    var radio_val = $('input[name="inv_type"]:checked', '#fund_sel_form').val(); 
    $('input[name="inv_type"]:checked','#fund_sel_form').find('label').find('img').attr('src','icons/r_en.png');
});

Try this working fiddle jsfiddle.net/s1f891vn/ 试试这个工作提琴jsfiddle.net/s1f891vn/

$('.monthly-radio-div input').on('change', function() {
    var parentDiv=$(this).parents(".monthly-radio-div:eq(0)");
    if($(this).is(":checked")){
        $(parentDiv).find("img").attr("src","https://cdn1.iconfinder.com/data/icons/toggle/512/radio-on-128.png");
    }
    else{
        $(parentDiv).find("img").attr("src","https://cdn1.iconfinder.com/data/icons/toggle/512/radio-off-128.png");
    }
});
$('.monthly-radio-div > input').on('change', function() {
    var par =$(this).parents().eq(0);
    if($(this).is(":checked")){
        $(parentDiv).find("img").attr("src","image.png");
    }
    else{
        $(parentDiv).find("img").attr("src","image.png");
    }
});

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

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