【问题标题】:Replacing radio buttons with different images用不同的图像替换单选按钮
【发布时间】:2010-06-24 21:51:12
【问题描述】:

我需要创建一组 2 个单选按钮(选项:男性/女性)来显示图像而不是实际按钮。图像应在选择时更改。所以应该有4个图像:男性(开),男性(关),女性(开),女性(关)。

我在整个网站中都使用 jQuery,如果可能的话,我也想为此使用 jQuery。

我找到了各种替换表单控件的插件,我正在使用imageTick 替换复选框,它工作正常。但是,我不知道如何调整插件,所以我可以在一个单选按钮组中使用不同的图像。

谢谢!

【问题讨论】:

    标签: javascript jquery forms


    【解决方案1】:

    我修改了插件以满足您的需求。它现在可以根据每个单选按钮的状态显示自定义图像。如果您发现任何错误,请发表评论:)

    演示:http://jsfiddle.net/mctcs/

    使用(用于名为gender 的单选框,选项值为malefemale):

    $("input[name='gender']").imageTick({
        tick_image_path: { 
            male: "images/gender/male_checked.jpg", 
            female: "images/gender/female_checked.jpg"
            //"default": "images/gender/default_checked.jpg" //optional default can be used
        },
        no_tick_image_path: { 
            male: "images/gender/male_unchecked.jpg", 
            female: "images/gender/female_unchecked.jpg"
            //"default": "images/gender/default_unchecked.jpg" //optional default can be used
        },
        image_tick_class: "gender",
    });
    

    插件来源:

    /******************************************
    
    Image Tick v1.0 for jQuery
    ==========================================
    Provides an unobtrusive approach to image
    based checkboxes and radio buttons
    ------------------------------------------
    by Jordan Boesch
    www.boedesign.com
    June 8, 2008
    
    
    Modified June 25, 2010:
    - Radio buttons can have individual images
    by Simen Echholt
    http://stackoverflow.com/questions/3114166/#3114911
    ******************************************/
    
    (function($){
    
        $.fn.imageTick = function(options) {
    
            var defaults = {
                tick_image_path: "images/radio.gif",
                no_tick_image_path: "no_images/radio.gif",
                image_tick_class: "ticks_" + Math.floor(Math.random()),
                hide_radios_checkboxes: false
            };
    
            var opt = $.extend(defaults, options);
    
            return this.each(function(){
    
                var obj = $(this);
                var type = obj.attr('type'); // radio or checkbox
    
                var tick_image_path = typeof opt.tick_image_path == "object" ?
                    opt.tick_image_path[this.value] || opt.tick_image_path["default"] :
                    opt.tick_image_path;
    
                var no_tick_image_path = function(element_id) {
                    var element = document.getElementById(element_id) || { value: "default" };
                    return typeof opt.no_tick_image_path == "object" ?
                        opt.no_tick_image_path[element.value] || opt.no_tick_image_path["default"]:
                        opt.no_tick_image_path;
                }
    
                // hide them and store an image background
                var id = obj.attr('id');
                var imgHTML = '<img src="' + no_tick_image_path(id) + '" alt="no_tick" class="' + opt.image_tick_class + '" id="tick_img_' + id + '" />';
    
                obj.before(imgHTML);
                if(!opt.hide_radios_checkboxes){
                    obj.css('display','none');
                }
    
                // if something has a checked state when the page was loaded
                if(obj.attr('checked')){
                    $("#tick_img_" + id).attr('src', tick_image_path);
                }
    
                // if we're deadling with radio buttons
                if(type == 'radio'){
    
                    // if we click on the image
                    $("#tick_img_"+id).click(function(){
                        $("." + opt.image_tick_class).each(function() {
                            var r = this.id.split("_");
                            var radio_id = r.splice(2,r.length-2).join("_");
                            $(this).attr('src', no_tick_image_path(radio_id))
                        });
                        $("#" + id).trigger("click");
                        $(this).attr('src', tick_image_path);
                    });
    
                    // if we click on the label
                    $("label[for='" + id + "']").click(function(){
                        $("." + opt.image_tick_class).each(function() {
                            var r = this.id.split("_");
                            var radio_id = r.splice(2,r.length-2).join("_");
                            $(this).attr('src', no_tick_image_path(radio_id))
                        });
                        $("#" + id).trigger("click");
                        $("#tick_img_" + id).attr('src', tick_image_path);
                    });
    
                }
    
                // if we're deadling with checkboxes
                else if(type == 'checkbox'){
    
                    $("#tick_img_" + id).click(function(){
                        $("#" + id).trigger("click");
                        if($(this).attr('src') == no_tick_image_path(id)){
                            $(this).attr('src', tick_image_path);
                        }
                        else {
                            $(this).attr('src', no_tick_image_path(id));
                        }
    
                    });
    
                    // if we click on the label
                    $("label[for='" + id + "']").click(function(){
                        if($("#tick_img_" + id).attr('src') == no_tick_image_path(id)){
                            $("#tick_img_" + id).attr('src', tick_image_path);
                        }
                        else {
                            $("#tick_img_" + id).attr('src', no_tick_image_path(id));
                        }
                    });
    
                }
    
            });
        }
    
    })(jQuery);
    

    【讨论】:

    • 非常感谢!完美运行:)
    【解决方案2】:

    你当然可以伪造它:

    HTML:

    <ul>
        <li><span class="radio">Value 1</span> Label 1</li>
        <li><span class="radio">Value 2</span> Label 2</li>
        <li><span class="radio">Value 3</span> Label 3</li>
    </ul>
    <input type="submit" />
    

    jQuery:

    $(function(){
        $('.radio').live('click', function(){
            $('.radio').removeClass('selected');
            $(this).addClass('selected');
        });
        $('input[type=submit]').live('click', function(){
            var data = { "myRadioValue" : $('.radio.selected').text() };
            $.post('myurl.com', data, function(result){ 
                console.log('hooray!', result); 
            });
        });
    });
    

    然后,您可以通过 CSS 执行任何您需要的操作,以根据存在的类应用适当的图像。

    【讨论】:

    • 我认为在处理输入字段时,优雅的降级/渐进增强是最重要的。虽然这个答案可能在一定程度上有效,但存在几个可用性问题(例如:它们无法获得焦点,只能通过鼠标点击)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-04
    • 2011-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-12
    相关资源
    最近更新 更多