脚本在IE 9中不起作用

时间:2012-08-20 14:49:47

标签: javascript jquery jsp internet-explorer-9

我有一个用于检查复选框的脚本,它在firefox和google chrome中运行良好,但它在IE 9中不起作用

    $notifyBySmsCheckbox
  .change(function(){
    if (this.checked) {console.log('checked');
      $('#f_notify_by_sms_instructions, #f_notify_by_sms_pin, #cellphoneInfo').show();
    }
    else {console.log('not checked');
      $('#f_notify_by_sms_instructions, #f_notify_by_sms_pin, #cellphoneInfo').hide();
    }
  })
  .change();

任何人都知道为什么这是??

3 个答案:

答案 0 :(得分:5)

..这很可能是因为IE没有控制台对象。尝试并删除co​​nsole.log,你会没事的!

答案 1 :(得分:3)

仅在定义时访问console

  $notifyBySmsCheckbox.change(function() {   
    if (this.checked) {
      console && console.log('checked');
      $('#f_notify_by_sms_instructions, #f_notify_by_sms_pin, #cellphoneInfo').show();
    } else {
      console && console.log('not checked');
      $('#f_notify_by_sms_instructions, #f_notify_by_sms_pin, #cellphoneInfo').hide();
    }
  })

此外,您可以使用toggle来简化代码,如@epascarello所示:

  $notifyBySmsCheckbox.change(function() {
    console && console.log(this.checked ? "checked" : "not checked");
    $('#f_notify_by_sms_instructions, #f_notify_by_sms_pin, #cellphoneInfo').toggle(this.checked);
  });

答案 2 :(得分:1)

...如果您在开放工具中开启工具,它将起作用,否则就不会。你需要支持8和7吗? - 如果是这样的话,你会遇到onchange事件的问题。请改用onClick。

相关问题