jquery:单选按钮替换和关键字匹配
我想用图像替换我的单选按钮,这是系统生成的 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”,则将此按钮替换为此 标记,该标记将是在 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>');
}
}
}
}
请给我一些想法来实现...谢谢。
I want to replace my radio buttons with images, here is my html which will be generated by the system,
<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>
I want the jquery script to check if the title text in radio input contains certain keywords or letters, then replace that radio button accordingly.
For instance, if it is found that radio button contains the keyword - 'Small', or 'small', or even 's', then replace this button with this <a>
tag which will be styled in css into an image,
<a href="#" id="button-s" class="button-size hide-text" title="Click this to select size Small">S</a>
and this will be the same case if the keyword - 'Medium', or 'medium' is found...
here is the jquery that I am stuck at,
if ($(':contains("Small")',$this_title).length > 0)
below is the script I have come out so far...
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>');
}
}
}
}
please give me some ideas to make it... thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个使用 正则表达式 来匹配标题并选择类型的解决方案
:可以在 jsFiddle 上查看上述内容。
Here's a solution using regular expressions to match the title and pick out the type:
You can see the above in action on jsFiddle.
您只需要执行此操作
这将用图像替换标题包含单词“Small”的所有输入元素,或者更符合您的要求
You just need to do this
This will replace all input element which title contains word "Small" with an image or to be more in line with your requirement