最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - IE error .JavaScript runtime error: Object doesn't support property or method 'preventDefault'

programmeradmin2浏览0评论

I have the following code inside my asp mvc web application:-

<script>
function validateForm(e) {
    if ($("[name=ip]").val() == "" && $("[name=mac]").val() == "") jAlert('Please enter atleast one search value.', 'Message');
    e.preventDefault();
}
 </script> 

but when accessing this Script on IE i will get the following error:-

0x800a01b6 - JavaScript runtime error: Object doesn't support property or method 'preventDefault'

while when accessing the web page that uses this script using Firefox, chrome it will work fine, can anyone advice please ?

I have the following code inside my asp mvc web application:-

<script>
function validateForm(e) {
    if ($("[name=ip]").val() == "" && $("[name=mac]").val() == "") jAlert('Please enter atleast one search value.', 'Message');
    e.preventDefault();
}
 </script> 

but when accessing this Script on IE i will get the following error:-

0x800a01b6 - JavaScript runtime error: Object doesn't support property or method 'preventDefault'

while when accessing the web page that uses this script using Firefox, chrome it will work fine, can anyone advice please ?

Share edited Oct 30, 2013 at 15:42 Tushar Gupta - curioustushar 57.1k24 gold badges106 silver badges109 bronze badges asked Oct 30, 2013 at 15:37 John JohnJohn John 1 6
  • 4 how are you calling the function? – ThiefMaster Commented Oct 30, 2013 at 15:38
  • see this other stackoverflow question – Josiah Ruddell Commented Oct 30, 2013 at 15:39
  • 2 Internet Explorer's event object doesn't have a preventDefault() function, you'll need to set its returnValue property to false instead. javascripter/faq/eventpreventdefault.htm – Anthony Grist Commented Oct 30, 2013 at 15:40
  • 2 @AnthonyGrist: Or use jQuery to bind the event. – gen_Eric Commented Oct 30, 2013 at 15:41
  • 1 @RocketHazmat Or that! I didn't notice the jQuery tag on the question. – Anthony Grist Commented Oct 30, 2013 at 15:42
 |  Show 1 more ment

2 Answers 2

Reset to default 3
//for IE
e.returnValue = false;

//for browsers supporting preventDefault()
if(e.preventDefault) e.preventDefault();

or short record:

(e.preventDefault) ? e.preventDefault() : e.returnValue = false;

IE doesn't always like preventDefault

Check to make sure the broswer like preventDefault, if it doesnt, use returnValue.

if(e.preventDefault) {
   e.preventDefault();
} else {
   e.returnValue = false;
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论