I have a simple HTML button in my form with a script like this:
$(document).ready(function () { $("#btn1").click(function () { $("#btn1").text("Button clicked"); return false; }); });
With return falseit it works as I expect - I press a button and its text changes to "Button clicked". Without "return false", it changes, but then changes back.
return false
End jQuery noob, why do I need "return false"?
If it was <button>not intended to submit the form, instead of using return false;or other workarounds, make the button the correct type.
<button>
return false;
<button id="btn1" type="button">
. , , , - ( 3 : submit, button reset).
A <button> , , javascript, .
return false .
. <button> submit , , .
submit
Like @adeneo, your form submits, so the page reloads. In addition, if you do not want to use return false;, you can use preventDefault()by passing the event parameter to your function as such:
preventDefault()
$(document).ready(function () { $("#btn1").click(function (ev) { ev.preventDefault(); $("#btn1").text("Button clicked"); }); });
Hope this helps,
Source: https://habr.com/ru/post/1569777/More articles:How to check if an image object matches a resource? - c #Replace document attributes in publish function - javascriptAdding plug-in parameters via config.xml in Visual Studio does not work - cordovaSpecifying app_id and app_name for facebookConnect in a hybrid application with multiple devices in VS2013 and W8.1 - visual-studio-2013Add plugin with variables - cordovaTrying to make raw OpenGL in a QtQuick application, it looks like QQuickWindow :: setClearBeforeRendering is not working? - qtHow to convert AM / PM date and time string to 24 hour timestamp mysql - dateWhy does setting up a console buffer raise an invalid descriptor exception when redirecting output? - c #Files.move REPLACE_EXISTING cannot be resolved by a variable - javaПочему mm_struct-> start_stack и vm_area_struct-> start не указывают на один и тот же адрес? - linuxAll Articles