Use images instead of radio buttons

后端 未结 8 1713
Happy的楠姐
Happy的楠姐 2020-11-22 16:47

If I have a radio group with buttons:

\"Image

... how can I show only images in the select option i

相关标签:
8条回答
  • 2020-11-22 17:34

    Here is a simple jQuery UI solution based on the example here:

    http://jqueryui.com/button/#radio

    Modified code:

    <!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <title>jQuery UI Button - Radios</title>
      <link rel="stylesheet" href="//code.jquery.com/ui/1.11.3/themes/smoothness/jquery-ui.css">
      <script src="//code.jquery.com/jquery-1.10.2.js"></script>
      <script src="//code.jquery.com/ui/1.11.3/jquery-ui.js"></script>
      <link rel="stylesheet" href="/resources/demos/style.css">
      <script>
      $(function() {
        $( "#radio" ).buttonset();
      });
      </script>
    </head>
    <body>
    
    <form>
      <div id="radio">
        <input type="radio" id="radio1" name="radio"><label for="radio1"><img src="image1.gif" /></label>
        <input type="radio" id="radio2" name="radio" checked="checked"><label for="radio2"><img src="image2.gif" /></label>
        <input type="radio" id="radio3" name="radio"><label for="radio3"><img src="image3.gif" /></label>
      </div>
    </form>
    
    </body>
    </html>
    

    jQueryUI takes care of the image background so you know which button is checked.

    Beware: If you want to set a button to checked or unchecked via Javascript, you must call the refresh function:

            $('#radio3').prop('checked', true).button("refresh");
    
    0 讨论(0)
  • 2020-11-22 17:38

    Here is very simple example

    input[type="radio"]{
       display:none;
    }
    
    input[type="radio"] + label
    {
        background-image:url(http://www.clker.com/cliparts/c/q/l/t/l/B/radiobutton-unchecked-sm-md.png);
        background-size: 100px 100px;
        height: 100px;
        width: 100px;
        display:inline-block;
        padding: 0 0 0 0px;
        cursor:pointer;
    }
    
    input[type="radio"]:checked + label
    {
        background-image:url(http://www.clker.com/cliparts/M/2/V/6/F/u/radiobutton-checked-sm-md.png);
    }
    <div>
      <input type="radio" id="shipadd1" value=1 name="address" />
      <label for="shipadd1"></label>
      value 1
    </div>
    
    <div>
      <input type="radio" id="shipadd2" value=2 name="address" />
      <label for="shipadd2"></label>
      value 2
    </div>

    Demo: http://jsfiddle.net/La8wQ/2471/

    This example based on this trick: https://css-tricks.com/the-checkbox-hack/

    I tested it on: chrome, firefox, safari

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