Home  >  Article  >  Web Front-end  >  IE8下Jquery获取select选中的值post到后台报错问题_jquery

IE8下Jquery获取select选中的值post到后台报错问题_jquery

WBOY
WBOYOriginal
2016-05-16 16:42:471378browse

我们一般使用jquery获取select时,一般这么用:

<select id='a'> 
<option selected='selected' value='1'> 
</select> 
var selectedValue = $("#a").val();

在非IE8下,selectedValue的值为“1”,typeof selectedValue 为“string”。

在IE8下,selectedValue的值为[“1”],typeof selectedValue 为 “objectg”。

如果直接将selectedValue post发送到后台,后台接收时会报错,因为在传输过程中,IE8下selectedValue当成了数组,后台无法识别。

解决的代码如下:

selectedValue = typeof selectedValue == "object" &#63; selectedValue[0] : selectedValue;

 这样selectedValue为字符串了。

​另外这样会引发其他的问题:

var a = selectedValue.trim();

这段代码在IE8下无法执行,可能的原因也是由于上述所致。

​使用如下代码就确保可以运行:

$.trim(selectedValue);
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn