0

我想用图像替换我的单选按钮,这是我的 html 将由系统生成,

<ul>
    <li><input type="radio" name="id" value="68135112" title="Default / Default Title / Small" class="button-replace-size"/></li>
    <li><input type="radio" name="id" value="70365102" title="Default / Default Title / Medium" class="button-replace-size"/></li>
    <li><input type="radio" name="id" value="70365152" title="Default / Default Title / Large" class="button-replace-size"/></li>
    <li><input type="radio" name="id" value="70365162" title="Default / Default Title / Extra Large" class="button-replace-size"/></li>
</ul>

我希望 jquery 脚本检查单选输入中的标题文本是否包含某些关键字或字母,然后相应地替换该单选按钮。

例如,如果发现单选按钮包含关键字 - 'Small',或'small',甚至's',则将此按钮替换为此<a>标签,该标签将在css中设置为图像,

<a href="#" id="button-s" class="button-size hide-text" title="Click this to select size Small">S</a>

如果找到关键字 - 'Medium' 或 'medium' 这将是相同的情况......

这是我被困在的jquery,

if ($(':contains("Small")',$this_title).length > 0)

下面是我到目前为止出来的脚本......

  this.replace_element_radio_size = function(){

    /* radio button replacement - loop through each element */
    $(".button-replace-size").each(function(){

        if($(this).length > 0)
        {
            $(this).hide();
            var $this = $(this);
            var $this_title = $this.attr('title');

            if ($(':contains("Small")',$this_title).length > 0)
            {
                 $(this).after('<a href="#" id="button-s" class="button-size hide-text" title="Click this to select size Small">S</a>');


            }
        }
    }
}

请给我一些想法来使它...谢谢。

4

2 回答 2

0

你只需要这样做

$('input[title*="Small"]').replaceWith("<img src=sample.jpg>")

这将用图像替换标题包含单词“Small”的所有输入元素,或者更符合您的要求

$('input[title*="Small"]').replaceWith('<a href="#" id="button-s" class="button-size hide-text" title="Click this to select size Small">S</a>')
于 2011-01-14T01:31:00.180 回答
0

这是使用正则表达式匹配标题并挑选类型的解决方案:

$(function(){

    var replacement = $('<a href="#" class="button-size hide-text"></a>');

    $('input:radio').each(function(){
        var $this = $(this),
            type = $this.attr('title').match(/Default Title \/ (.+)$/);

        if (type.length > 1) {
            var shortType = type[1].substring(0,1).toLowerCase();
            $(this).replaceWith(
                replacement.clone()
                    .attr('id', 'button-'+shortType)
                    .attr('title', 'Click here to select '+type[1])
                    .text(shortType)
            );
        }
    });

});

您可以在jsFiddle上看到上述内容。

于 2011-01-14T01:43:43.023 回答