jquery 'change' event not firing on Image-

2019-08-01 01:30发布

Here is an example : http://jsfiddle.net/c8uywnpa/21/ which works on other browsers but IE (10). The issue comes when a the buttonset is wrapped within a 'form' element. Please see example included.

<b> Form - Click on image does not work, must click on number </b>
<form>
<div id="myRadio">
    <input id="rf1" type="radio" name="myRadio" value="1" />
    <label for="rf1">1 <img src="//jsfiddle.net/img/initializing.png" width="50"/> </label>
    <input id="rf2" type="radio" name="myRadio" value="2" /> 
    <label for="rf2">2 <img src="//jsfiddle.net/img/initializing.png" width="50"/> </label>
</div>
</form>
<hr>
<h3> No Form - Click on image works </h3>
<div id="myRadioNoForm">
    <input id="r1" type="radio" name="myRadioNoForm" value="1" />
    <label for="r1">1 <img src="//jsfiddle.net/img/initializing.png" width="50"/> </label>
    <input id="r2" type="radio" name="myRadioNoForm" value="2" /> 
    <label for="r2">2 <img src="//jsfiddle.net/img/initializing.png" width="50"/> </label>
</div>

$("#myRadio").buttonset().on('change', function(event) {
    alert($(this).find(":checked").val()); 
});
$("#myRadioNoForm").buttonset().on('change', function(event) {
    alert($(this).find(":checked").val()); 
});

Clicking on the numbers in the label fires the change event but does not if you click on the Image in the label. Any help is much appreciated.

1条回答
甜甜的少女心
2楼-- · 2019-08-01 02:11

I had the same problem. I'm not using jQuery. I had the image wrapped with the span, so I tried to add an :after pseudoelement to it. It covers the image, so when clicking, you click on the pseudoelement rather than on the image. And it helped for me. ie9-11

.image { position: relative; display: block;}
.image:after {content:""; position:absolute; top:0; left:0; width:100%; height:100%;}


<label for="answ">
    <span class="image"><img src="image.jpg"></span>
</label>
查看更多
登录 后发表回答