Clickable label not working in IE 8

后端 未结 9 2104
Happy的楠姐
Happy的楠姐 2020-12-14 01:47

I\'ve got the following list item:

  • 相关标签:
    9条回答
    • 2020-12-14 02:16

      I tried some of the other solutions posted here and eventually amended them to get a working fix to your problem. My problem was similar, though the HTML looked a little different, with the LABEL tag wrapping both the image and the INPUT radio button.

      The solution I include below will ensure that any onClick handlers fire correctly on the radio button itself. It also does this only once and not in browsers where the radio button label behaves normally.

      This is the jQuery code I used to solve the problem in Internet Explorer 11:

      $('LABEL > IMG').click(function () {
          var inputId = $(this).parents('LABEL:first').attr("for");
          if (inputId) $('#' + inputId).not(':checked').attr('checked', true).click();
      });
      

      The code above uses the jQuery method attr() to manipulate the checked property. If you're using jQuery 1.6 or higher you should modify this to use the jQuery method prop() instead.

      0 讨论(0)
    • 2020-12-14 02:18

      This works for me with a group of radio inputs followed by labels containing an image. Basically used it instead of using for attribute in all browsers

            inputs.each(function() {
      
               $(this).click(function (e) {
                   // do stuff ...
               });
      
              // adding click handler to label containing image 
              $(this).next().on('click', function(event) {
                  // prevent 'for' attribute firing
                  event.preventDefault();
                  // check and trigger click on input
                  $(this).prev().prop("checked", true).trigger('click');
              });
           });
      
      0 讨论(0)
    • 2020-12-14 02:20

      There's a slightly cleaner approach to change the markup that doesn't involve (ugly) CSS hacks or Javascript; change the <img> tag to a <span> tag with a background-image of the appropriate size. For example:

      <li>
        <input value="someRadioButton" name="ctl00$mainContent$group" type="radio"
               id="ctl00_mainContent_somelRadioButton" onclick="showSomeInfo()" />
        <label for="ctl00_mainContent_someRadioButton">
          <span class="extraPadding-Right-10" style="background-image: url(https://xxy.com/some_mark_37x23.gif); width: 100px; height: 100px; display: inline-block" />
        </label>
      </li>
      

      Replacing the width and height of your image appropriately.

      0 讨论(0)
    提交回复
    热议问题