最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Option in select with span text - Stack Overflow

programmeradmin2浏览0评论

External system generates translations and replace literals with span text on the page. It works perfectly fine for the most of places but it doesn't work with options in select. They support only text. As the result my page has the issue like here SQL Fiddle sample.

<select class="ProductInfo" >
    <option value=""></option>
    <option value="0">&lt;span class='translation'&gt;Opt1&lt;/span&gt;</option>
    <option value="1">&lt;span class='translation'&gt;Opt2&lt;/span&gt;</option>
</select>

I want some jquery/javascript function that would replace option content with just text and remove wrapper in above.

Expected result:

<select class="ProductInfo" >
    <option value=""></option>
    <option value="0">Opt1</option>
    <option value="1">Opt2</option>
</select>

External system generates translations and replace literals with span text on the page. It works perfectly fine for the most of places but it doesn't work with options in select. They support only text. As the result my page has the issue like here SQL Fiddle sample.

<select class="ProductInfo" >
    <option value=""></option>
    <option value="0">&lt;span class='translation'&gt;Opt1&lt;/span&gt;</option>
    <option value="1">&lt;span class='translation'&gt;Opt2&lt;/span&gt;</option>
</select>

I want some jquery/javascript function that would replace option content with just text and remove wrapper in above.

Expected result:

<select class="ProductInfo" >
    <option value=""></option>
    <option value="0">Opt1</option>
    <option value="1">Opt2</option>
</select>
Share Improve this question asked Jan 25, 2016 at 4:09 dcieslakdcieslak 2,7151 gold badge14 silver badges20 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

It is best to fix in the template itself, if that is not possible you can try something like

$('.ProductNoInfo option').text(function(i, t) {
  return $(t).text()
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="ProductNoInfo">
  <option value=""></option>
  <option value="0">&lt;span class='translation'&gt;Opt1&lt;/span&gt;</option>
  <option value="1">&lt;span class='translation'&gt;Opt2&lt;/span&gt;</option>
</select>

Try using decodeURICompoenent

$("select option").each(function() {
  this.textContent = $(decodeURIComponent(this.textContent)).text()
})
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select class="ProductInfo" >
    <option value=""></option>
    <option value="0">&lt;span class='translation'&gt;Opt1&lt;/span&gt;</option>
    <option value="1">&lt;span class='translation'&gt;Opt2&lt;/span&gt;</option>
</select>

Try this,

$('.ProductNoInfo option').each(function(){
   $(this).text($(this).find('span').text());
});
    <html>
<head>
<script src="http://code.jquery./jquery-1.10.2.js"></script>

<script>

    $(document).ready(function(){  
        $('.ProductInfo option').each(function () {
            this.textContent = $(decodeURIComponent(this.textContent)).text()
        });


        });  
</script>
</head>
<body>

 <select class="ProductInfo" >
    <option value=""></option>
    <option value="0">&lt;span class='translation'&gt;Opt1&lt;/span&gt;</option>
    <option value="1">&lt;span class='translation'&gt;Opt2&lt;/span&gt;</option>
</select>
</body>
</html>
发布评论

评论列表(0)

  1. 暂无评论