为什么IE 11 JavaScript会在value方法中剥离空格? [英] Why does IE 11 javascript strips spaces in value method?

查看:70
本文介绍了为什么IE 11 JavaScript会在value方法中剥离空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当具有Ø-&"的选项值时在选择标签中显示并在警报IE 11(11.0.9600.17126)中显示选定的值似乎会去除空格并将其显示为ø-&"你知道为什么吗?

When having the option value of "ø- &" in a select tag and displaying the selected value in an alert IE 11 (11.0.9600.17126) seems to strip spaces and displays it as "ø-&" do you know why?

HTML:

<select id="companies">
       <option>ø- &</option>
       <option>ø- &amp;</option>
       <option>&oslash;- &amp;</option>
</select>
<button onclick="javascript:alert(document.getElementById('companies').value);">TEST</button>

JSFiddle: http://jsfiddle.net/aK35t/14/

JSFiddle: http://jsfiddle.net/aK35t/14/

推荐答案

这仅仅是因为您正在读取该选项的值-但该值中的空格实际上并不是有效的.相反,您想阅读文本:

That's simply because you're reading the value of the option - but spaces aren't really valid inside the value. Instead, you want to read the text:

document.getElementById('companies')
.options[document.getElementById('companies').selectedIndex].text

但是,无论如何,您都不应该依赖自动的文本到价值".如果您手动设置选项的值,那么它也可以与您的原始代码配合使用:

However, you shouldn't rely on automatic "text to value" anyway. If you manually set the value of the option, it will work fine with your original code as well:

<select id="companies">
    <option value="ø- &amp;">ø- &amp;</option>
</select>
<button onclick="alert(document.getElementById('companies').value);">GET</button>

尽管如此,您仍然很可能在选项值中有一些ID-因此,如果您想在javascript中获取文本,请使用上面的代码段方式(或使用jQuery的 text())

You'll most likely have some ID in the option value anyway, though - so if you want to get the text in javascript, use the snippet way above (or use jQuery's text()).

这篇关于为什么IE 11 JavaScript会在value方法中剥离空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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