如何删除下拉列表中的部分文本? [英] How to remove part of text in a dropdown list?

查看:104
本文介绍了如何删除下拉列表中的部分文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个聪明的模板,我可以编辑HTML并只放置JavaScript,但不能编辑php代码。



清单如下所示:

 <选择> 
< option id =1value =first> {$ line1}< / option>
< option id =2value =second> {$ line2}< / option>
< / select>

在实时页面中, {$ line1} 显示为绿色 {$ line2} 显示为黑白

我想放置一个JavaScript或jQuery代码来取出(移除)和white 来自 {$ line2} 的文本,以便仅显示黑色。有什么建议么?您的答案将非常感谢。

解决方案

这是一个动态解决方案:

  $('select option')。each(function(i,item){
var words = $(item).text()。split('') ;
$(item).text(words [0]);
});

演示:小提琴


I'm working on a smarty template which I can edit HTML and place JavaScript only, but cannot edit the php code.

The list looks like this:

<select>
  <option id="1" value="first">{$line1}</option>
  <option id="2" value="second">{$line2}</option>
</select>

Where in the live page, {$line1} displayed as green and {$line2} displayed as black and white.

I want to place a JavaScript or jQuery code to take out (remove) the and white text from {$line2} in order to show the black only. Any suggestions? Your answers would be much appreciated.

解决方案

Here is a dynamic solution:

$('select option').each(function(i, item){
        var words = $(item).text().split(' ');
        $(item).text(words[0]);
    });

Demo: Fiddle

这篇关于如何删除下拉列表中的部分文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆