Home > Web Front-end > JS Tutorial > body text

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

WBOY
Release: 2016-05-16 16:42:47
Original
1377 people have browsed it

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

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

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

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

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

解决的代码如下:

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

这样selectedValue为字符串了。

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

var a = selectedValue.trim();
Copy after login

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

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

$.trim(selectedValue);
Copy after login
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!