Jquery gets the selected value of select and posts it to the background to report an error
- 2020-03-30 03:30:59
- OfStack
This is how we usually use jquery to get a select:
<select id='a'>
<option selected='selected' value='1'>
</select>
var selectedValue = $("#a").val();
Under non-ie8, the selectedValue value is "1" and the typeof selectedValue is "string".
Under IE8, the selectedValue is [" 1 "] and the typeof selectedValue is "objectg".
If the selectedValue post is directly sent to the background, an error will be reported when the background receives it, because in the transmission process, the selectedValue under IE8 is used as an array, and the background cannot recognize it.
The solution code is as follows:
selectedValue = typeof selectedValue == "object" ? selectedValue[0] : selectedValue;
So the selectedValue is a string.
The & # 8203; And that can lead to other problems:
var a = selectedValue.trim();
This code cannot be executed under IE8, the possible reason is also due to the above.
The & # 8203; Use the following code to ensure that it can be run:
$.trim(selectedValue);