【问题标题】:jquery: radio button replacement and keyword matchingjquery:单选按钮替换和关键字匹配
【发布时间】:2011-01-14 01:03:22
【问题描述】:

我想用图片替换我的单选按钮,这是系统生成的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',则将此按钮替换为&lt;a&gt;标签,该标签将在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>');


            }
        }
    }
}

请给我一些想法...谢谢。

【问题讨论】:

  • 请参阅下面的答案 - 只需注意 $(':contains("Small") 将选择包含文本“Small”的任何节点,而不是每个属性包含文本的节点。

标签: jquery title contains image-replacement


【解决方案1】:

这是一个使用regular expressions匹配标题并挑选类型的解决方案:

$(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 上看到上述内容。

【讨论】:

  • @lauthiamkok:没问题!我已经更新了它以添加正确的 ID 和文本,因为我看到你问过 Michal。
  • @Marcus Ekwall:非常感谢您的快速回复! :)
  • @Marcus Ekwall:抱歉,我还有一个问题刚刚出现!有时我有这个标题=“默认/默认标题/小”,有时我可能会得到这个标题=“默认标题/小”我怎样才能在这一行中适应这两种可能性 - .match(/^Default \/ Default Title \ / (.+)$/); ?谢谢。
  • @lauthiamkok:我已经更新了示例和小提琴以允许这样做!
  • @Marcus Ekwall:非常感谢。明天会看看细节。现在需要睡觉了...!谢谢。
【解决方案2】:

你只需要这样做

$('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>')

【讨论】:

  • 谢谢。问题是“大”和“超大”将输出相同的图像替换,即“大”图像替换。我怎样才能完全匹配关键字?
  • 是的,您可以使用 $('input[title~="XLarge"]')。 (您将需要更改属性)这将匹配两侧由空格分隔的整个单词。更多信息在这里api.jquery.com/attribute-contains-word-selector。就我个人而言,我不会使用标题,而是使用类属性来进行替换。你可以有
    并且你的选择器会更好: $(".size-small").replaceWith(...)
猜你喜欢
  • 2012-02-29
  • 2020-05-20
  • 2011-11-27
  • 1970-01-01
  • 1970-01-01
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
  • 2012-10-15
相关资源
最近更新 更多