preventdefault

event.preventDefault doesn't seem to work

末鹿安然 提交于 2019-12-02 14:09:06
So im trying to stop a form from refreshing the page and this is the code i'm using: $("#getName").submit(function(refresh) { refresh.preventDefault(); $("#p22").fadeOut(50); $("#p23").fadeIn(800); document.getElementById("p23").innerHTML = "Oh " + userName + ", alright i wont forget that!"; }) I can't seem to get this working properly.... id = getName - is the form id Michail Michailidis Your question is similar to: https://stackoverflow.com/a/6462306/986160 Try this: $("#getName").submit(function(refresh) { $("#p22").fadeOut(50); $("#p23").fadeIn(800); $("#p23").html("Oh " + userName + ",

preventDefault() won't work for me

做~自己de王妃 提交于 2019-12-02 05:48:10
Why will this refuse to work? HTML stuff <div id="nav-bar"> <ul> <li> <span> <a href="contact.html">Contact</a> </span> </li> </ul> </div> Javascript stuff $('div#nav-bar').filter('a').click(function(event){ event.preventDefault(); }); Filter only filters what is already selected. In your case, the #nav-bar element. You need this: $('div#nav-bar a').click(function(event){ event.preventDefault(); }); oezi filter is the wrong method to use here. you should either use find to look for elements in a selection: $('div#nav-bar').find('a')... or simply combine that into one selector: $('div#nav-bar a

Form still submits on preventDefault

我怕爱的太早我们不能终老 提交于 2019-12-02 05:19:07
I am using jQuery to submit form data to a file in the form's action. However, when I submit the form, the browser redirects to the action (comment.php). I don't know why this is because e.preventDefault() should stop the browser's redirection. Ideally, I would like the browser to not redirect and remain on the current page (index.php). jQuery: <script type='text/javascript'> $('document').ready(function(){ $('.commentContainer').load('../writecomment.php'); $('.answerContainer').on('submit', 'form', function(e){ e.preventDefault(); var form = $(this).closest('form'); $.post($(form).attr(

If conditions false then prevent default

给你一囗甜甜゛ 提交于 2019-12-01 15:59:05
I have a link. When some one clicks on that I want to check some conditions before letting it work. If it's false the default action should be prevented. $(".pager-next a.active").click(function(event) { if (!a == 1) { event.preventDefault(); } }); The link should only work if a is equal to 1 . Is the above code correct. a is set to 1 if a particular condition is met. The link should only work if the condition is met. Assuming by ' should only work if a is equal to 1 ' you mean the text of the a element is equal to 1, try this: $(".pager-next a.active").click(function(event) { if ($(this).text

Opera preventDefault() on keydown event

送分小仙女□ 提交于 2019-12-01 06:07:55
I'm trying to embed some keybindings in my webapp, and I'm having hard times with Opera. I have this code: window.onkeydown = function(e){ var key = e.keyCode ? e.keyCode : e.charCode ? e.charCode : false; if (e.ctrlKey && key === 84) { alert("foo"); e.preventDefault(); // return false; } } It works like a charm in Firefox and Chrome, but Opera still opens new tab. Same happens with return false; . My info: Opera/9.80 (X11; Linux i686; U; en) Presto/2.7.62 Version/11.00 Marcel Korpel Opera doesn't support preventDefault on keydown , only on keypress . As you can see in this example , you

JQuery PreventDefault and IE8 clarification

ぐ巨炮叔叔 提交于 2019-12-01 05:12:07
问题 I have been trying to understand why sometimes IE8 doesn't like PreventDefault and why sometimes it seems to be OK (no errors). From what I have read, including here at SO is that events in jquery are normalised so preventDefault will always exist with a jQuery event. However regular javascript event bindings is when the following workaround is needed for ie8: event.preventDefault ? event.preventDefault() : event.returnValue = false Is this correct? So if you are using jQuery .click .bind .on

Opera preventDefault() on keydown event

六眼飞鱼酱① 提交于 2019-12-01 03:46:40
问题 I'm trying to embed some keybindings in my webapp, and I'm having hard times with Opera. I have this code: window.onkeydown = function(e){ var key = e.keyCode ? e.keyCode : e.charCode ? e.charCode : false; if (e.ctrlKey && key === 84) { alert("foo"); e.preventDefault(); // return false; } } It works like a charm in Firefox and Chrome, but Opera still opens new tab. Same happens with return false; . My info: Opera/9.80 (X11; Linux i686; U; en) Presto/2.7.62 Version/11.00 回答1: Opera doesn't

preventDefault() not working for change event

给你一囗甜甜゛ 提交于 2019-12-01 03:35:58
Any ideas why preventDefault is not working? Here's the code below . . . Tks! <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> $(document).ready(function() { $("#text1").change(function(e) { e.preventDefault(); }); }); function myFunc() { alert("Random, annoying alert"); } </script> </head> Just one HTML element in the form: <body> <form name="test" method="post"> <input name="text1" id="text1" type="text" onchange="myFunc();"> </form> </body> Fiambre You can’t use preventDefault on change events because it’s not cancelable: $("#text1").change(function(e)

preventDefault() not working for change event

£可爱£侵袭症+ 提交于 2019-11-30 23:17:37
问题 Any ideas why preventDefault is not working? Here's the code below . . . Tks! <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> $(document).ready(function() { $("#text1").change(function(e) { e.preventDefault(); }); }); function myFunc() { alert("Random, annoying alert"); } </script> </head> Just one HTML element in the form: <body> <form name="test" method="post"> <input name="text1" id="text1" type="text" onchange="myFunc();"> </form> </body> 回答1: You

Cross browser preventDefault() without jQuery

筅森魡賤 提交于 2019-11-30 18:36:33
I wrote this bind method and am having an issue in my preventDefault() method to work in IE. The callback line never executes. Can someone provide assistance? Thanks! var preventDefault = function (event) { if (window.event) { window.event.returnValue = false; } else if (event.preventDefault) { event.preventDefault(); } else { event.returnValue = false; } }; var bindEvent = function (ele, type, cb) { if (window.addEventListener) { ele.addEventListener(type, cb, false); } else if (window.attachEvent) { ele.attachEvent('on' + type, function () { event.preventDefault = function () {