为什么.change不能用于IE8

Mat*_*iby 1 javascript jquery internet-explorer

好的,我有一个jQuery .change

$('#application').change(function(){
var selected = $(this).val().trim();
alert(selected);
if(selected == 'something') {
  $('#profile-info').show();
}
....
....
Run Code Online (Sandbox Code Playgroud)

我的HTML

<select name="application" id="application">
<option value="--select something--">--select something--</option>
<option value="first">first</option>
<option value="second">second</option>
<option value="third">third</option></select>
Run Code Online (Sandbox Code Playgroud)

警报会在所有浏览器上触发,但IE ..... ie8和7根本不起作用

任何想法我可能会失踪

SoW*_*Lie 5

这是因为IE9下面的任何内容都不支持"trim"方法.

如果你摆脱修剪,它工作得很好:

$('#application').change(function(){
    var selected = $.trim($(this).val());
    alert(selected);
    if(selected == 'something') {
      $('#profile-info').show();
    }
});
Run Code Online (Sandbox Code Playgroud)

检查IE开发人员工具中的错误控制台(按F12)以查看您收到的javascript错误.

看看我在IE8中工作的例子:http://jsfiddle.net/Ku8HL/1/.

编辑:jQuery实际上有一个trim方法,所以你可以使用$ .trim来完成同样的事情.IE8及更低版本的String对象上没有trim方法.我已经更新了上面的代码和jsfiddle以包含jQuery trim方法.