How do I put a space character before option text

2019-01-09 08:44发布

In a drop down list, I need to add spaces in front of the options in the list. I am trying

<select>
<option>&#32;&#32;Sample</option>
</select>

for adding two spaces but it displays no spaces. How can I add spaces before option texts?

14条回答
兄弟一词,经得起流年.
2楼-- · 2019-01-09 09:24

I tried several of these examples, but the only thing that worked was using javascript, much like dabobert's, but not jQuery, just plain old vanilla javascript and spaces:

for(var x = 0; x < dd.options.length; x++)
{
    item = dd.options[x];
    //if a line that needs indenting
    item.text = '     ' + item.text; //indent
}

This is IE only. IE11 in fact. Ugh.

查看更多
叼着烟拽天下
3楼-- · 2019-01-09 09:25

i tried multiple things but the only thing that worked for me was to use javascript. just notice that i'm using the unicode code for space rather than the html entity, as js doenst know a thing about entities

$("#project_product_parent_id option").each(function(i,option){
  $option = $(option);
  $option.text($option.text().replace(/─/g,'\u00A0\u00A0\u00A0'))
});
查看更多
唯我独甜
4楼-- · 2019-01-09 09:25
&nbsp;

Can you try that? Or is it the same?

查看更多
Animai°情兽
5楼-- · 2019-01-09 09:26

As Rob Cooper pointed out, there are some compatibility issues with older browsers (IE6 will display the actual letters "& nbsp;"

This is how I get around it in ASP.Net (I don't have VS open so I'm not sure what characters this actually gets translated to):

Server.HtmlDecode("&nbsp;") 
查看更多
6楼-- · 2019-01-09 09:32

I was also having the same issue and I was required to fix this as soon as possible. Though I googled a lot, I was not able to find a quick solution.

Instead I used my own solution, though I am not sure if its appropriate one, it works in my case and exactly which I was required to do.

So when you add an ListItem in dropdown and you want to add space then use the following:-

Press ALT and type 0160 on your numeric keypad, so it should be something like ALT+0160. It will add a space.

ListItem("ALT+0160 ALT+0160 TEST", "TESTVAL")
查看更多
Ridiculous、
7楼-- · 2019-01-09 09:33

Server.HtmlDecode("&nbsp;") is the only one that worked for me.

Otherwise the chr are printed as text.

I tried to add the padding as a Attribute for the listitem, however it didnt affect it.

查看更多
登录 后发表回答