Submit forms. Form Submission

In this article, let's look at the methods, like the jQuery library is given to a web retailer for processing submissions.

For robots with pods in scenarios JavaScript browser Nadaє API (for example, the function addEventListener). Using this function, you can enter a code, which is necessary to type, if the browser for the designated element will generate the specified subdomain.

Processing Pods for Helping jQuery Methods

Function syntax on

Before that, proceed to the addition of the elements of the samples of the bottom, and the elements of the back of the head must be taken away. You can find out about those, how to know the need for elements on the side, in the jQuery article - Select elements.

In jQuery, you can put a sub (listener sub) on the first element using the on and one helper functions, as well as short entries on.

// function on .on (events, handler); // function one .one (events, handler); // events - pod_ya or the list of pod_ya through probіl, when these are necessary, it is necessary to have a turnaround handler (handler) // handler - function (podії handler) // short entry functions on .event(handler); // event - the name of the sub (you can quiz for processing only those subs, for which such a short record was created in jQuery)

The one function is checked only once, because it won't be checked out once.

For example, dodamo for an additional function on the sub-click for all elements with the btn class:

// vikoristannya in the capacity of an anonymous function $(". Btn"). On ( "click", function () (// dії, yakі vykonnі nі nastanny podії ... console.log ($ (this) .text ( ));)); // using a sizable function in the wrapper var myFunction = function () (console.log ($ (this) .text ());) $ ( ". Btn"). On("click", myFunction);

Guidance code, entries for short entries of the function on:

$(".btn"). Click (function () (// dії, yakі dії vykonanі nі stanny podії ... console.log ($ (this) .text ());));

Dodatkova information about podium

When submitting an application, you can recognize additional information about him. The transfer of all the information, and the Event object itself into the sampler, is dependent on the help of the first argument.

$("#Demo"). On ( "click", function (e) (// e - Event object, to remove additional information about the sub, which was // often defeated by the authority of the Event object e.preventDefault (); e.stopPropagation (); // move away the merging of the sub_ // e.type - remove the type of the sub_ // e.target - send to the element to which the sub_is set // e.currentTarget - send to the flow element (for which target The value of power, as a rule, is the function itself this. // e.currentTarget === this // e.which - key code (for mouse), button code or symbol (for keyboard) //e.pageX, e.pageY - coordinates to the cursor, to the top left corner of the document));

space of names

In jQuery, after specifying the name of the subname, you can optionally specify the scope of the names.

for example:

// click sub in namespace first $("# demo"). On ( "click.first", function () (console.log ( "1 click sample");)); // click sub in namespace second $("# demo"). On ( "click.second", function () (console.log ( "2 click handler");));

The expanse of names is the same as the river. You won't win, for example, if you need to call out in full agreement, and only with singing names.

// call to click in the namespace first $ ("# demo"). Trigger("click.first"); // click click in namespace second $("# demo"). Trigger("click.second");

So with yoga help you just see the songs of the foot:

// show the click-throughs in the name space first $ ("# demo"). off("click.first"); // see the click walkthroughs in the name space second $ ("# demo"). off("click.second");

The description and application of the trigger and off functions can be found in the article a trio below.

Transfer of additional data to obrobnik

If necessary, you can pass the data to the data collector (for additional input, an additional argument for the on function). Access to the transfer of data to the middle of the processor is provided through the Event object.

It works like this (butt):

...

How to put a sprat on one element

The butt of the vikoristanny of one piece for dekіlkoh (2 or more) is suitable:

$("#Id"). On ( "keyup keypress blur change", function (e) (console.log (e.type); // subtype)); // otherwise var myFunction = function () (...) $ ( "# id") .keyup (myFunction) .keypress (myFunction) .blur (myFunction) .change (myFunction);

For the skin under its own function:

$("#Id"). On ((mouseenter: function () (// click handler mouseenter ...), mouseleave: function () (// click handler mouseleave ...), click: function () (/ / click handler ...) ));

An example of a variation in jQuery, a few samples (functions) per one sub:

$("#Demo"). Click (function () (console.log ( "1 click sample");)); $("#Demo"). Click (function () (console.log ( "2 click handle");));

For example, it is possible to find out in such a blackness that you can follow this way:

var eventList = $._data($("#demo"), "events"); console.log(eventList);

Software wiki click

There are 2 methods for wiki code in jQuery:

  • trigger - click on the specified subdomain of the element.
  • triggerHandler - calls a handle to the handle, when the handle itself is not triggered.
$("#Header"). On ( "click", function () (console ("Place a click on the #header element");))); // programmatic wiki click on the click element $ ("# header"). Trigger("click"); // shorthand for this wiki $("# header"). click(); // wiki click on the reversed element $ ("# header"). TriggerHandler("click");

jQuery - Ready sidebar

The process of adding to a certain element of the document's signature should start to sound after the page is taken, so if the DOM tree of the document is already prompted. Otherwise, when writing the descriptions, you can turn around to the elements, which are not available on the side.

The shortest entry for the landing page in jQuery looks like this:

$ (Function () (// dії, yakі nebhіdno vykonat ії іѕlja vіslja documenta...));

Ale, you can win and more dovgu record:

$ (Document) .ready (function () (// dії, yakі nebhіdno vykonat n_sla zavkazhennya document ...));

jQuery - Pod_ya zavantazhennya (load)

Podiya load the browser generates an element, if the wines and all the contributions to the new element were more interesting. This subtype is only recognized for elements in any of the present URLs: image, script, iframe and window.

For example, viconate the function, if the page will be more occupied (including pictures):

$ (Window).on ( "load", function () (// after reloading the side ...));

For example, we will display a message in the console, if the image is assigned, it will be captured:

...

jQuery - Match the mouse

In jQuery, for the introduction of the mouse, it is most common to use the following subtitles:

  • mouse down
  • mouseup
  • click
  • mousemove
  • wheel
  • hover
  • mouseenter
  • mouse over
  • mouseleave
  • mouseout

jQuery - Sub click (click)

Podіya click є collapsible podієyu, it will be blamed after the generation of podіy mousedown and mouseup. The mousedown button is triggered if the indicator is hovering over the element and the mouse button is pressed. The mouseup button is enabled if the indicator is hovering over the element and the mouse button is enabled. A click prompt is generated when the cursor is over an element and the mouse button is pressed and released. Tsі podії can take away be-yakі HTML element And.

For example, let's look at a summary of podia onclick window element. When the current button is pressed, the sampler will display the number of the pressed key and the coordinates of the cursor:

$ (Window).on ( "click", function (e) (// processing sub-click ... console.log ( "Button pressed:" + e.which); // 1 - left button, 2 - middle button , 3 - permissions console.log( "Cursor coordinates: x =" + e.pageX + "; y =" + e.pageY);));

For example, let's look at onclick on all elements with the btn class:

$(".btn"). On ( "click", function () (// code of the handler for pressing the button ...)); A short notation for underpressure is: $ (". Btn"). click(function()(...));

For example, let's take a look at how you can grab a block after a certain hour after clicking:

...Catch the block in 5 seconds...
...

jQuery - hover hover


jQuery - What is a hover hover

Podіya when presented to the cursor є collapsible and folded from 2 podіy:

  • entry(mouseenter, mouseover);
  • leaving (mouseleave, mouseout).

Mouseenter and mouseleave in jQuery are listened to as mouseover and mouseout just so you don't blame them if the cursor is allowed to enter and override internal elements and listen for elements. In other words, mouseover and mouseout can be merged, but mouseenter and mouseleave cannot.

For example, change the color of the element to the list when dragged to a new cursor:

  • pen
  • olive
  • line
...

Tі zh yourself dії, ale vikoristannyam mouseover and mouseout:

$("Ul>li"). mouseover (function () (// when entering the element $ (this).css ( "color", "orange");)). mouseout (function () (// when leaving the element $ (this).css ( "color", "black");));

These methods are neobov'yazkovo vikoristovuvaty at once, you can also zastosovuvat okremo.

For example, pіdrahuєmo kіlkіst vіdvіduvan of the element at the present moment "Give me a bear":

Quantity: 0
...

The mouseenter and mouseleave tag can be tagged under the hover.

For example, we will rewrite the butt of the butt, vikoristovuyuchi hover:

$("Ul>li"). Hover (function () (// when entering the element $ (this) .css ( "color", "orange");), function () (// when leaving the element $ ( this) .css ( "color", "black");));

When hovering a hover in jQuery, the first one is beaten to set when the cursor enters the element (mouseenter), and the other one when the cursor leaves the element (mouseleave).

If you indicate one hover for a hover, then you will be victorious both for the aiming of the mouse, and for leaving the mouse.

for example:

$("H1"). Hover (function () (console.log ( "It became possible to log into the element or exit from the new one");));

jQuery - Move the mouse

The mousemove is superimposed on the element if the mouse pointer moves in the middle of it. Whether an HTML element can be taken into account.

$(".target"). Mousemove (function (e) (console.log ( "Mousemove click callback."); Console.log ( "Coordinates of the top left corner of the document:" + e.pageX + "," + e.pageY); console. log ( "Cursor coordinates in the middle of the label:" + e.offsetX + "," + e.offsetY);));

jQuery - Bear wheel

You can listen to the sound of scrolling the wheel (wheel) of the bear like this:

$ (Window) .on ( "wheel", function (e) (// code of the handler (for example) ... console.log ( "Number of scrolled pixels:" + e.originalEvent.deltaY); if (e.originalEvent. deltaY< 0){ console.log("Прокручиваем вверх"); } else { console.log("Прокручиваем вниз"); } });

Given a window on the scroll view, it is generated by the browser only for the little bear, with which the element no matter what is scrolled, so you can work with it on the overflow elements, equal to hidden. Another thing to consider is that wheel is generated before scrolling, and scroll is generated after it.

jQuery - Keyboard Submissions

When pressing the keys of the keyboard, the browser generates the input in the following order:

keydown -> keypress -> keyup

  • keydown (key pressed, but not yet released);
  • keypress (keypress is generated for letters, numbers and other keys, for a keypress) - it is recognized to select the code for the symbol (subjects keydown and keyup allow you to recognize only the code of the key, but not the symbol);
  • keyup (generated by the browser when a key is released).

For example, let's write a sample for listening to all the sounds that are heard when pressing the keys:

...

The butt, in which it is shown how it is possible to listen to the keypress and recognize, it is pressed that the keypress is shown:

$ (Document).keypress( "c", function (e) (if (e.ctrlKey) (console.log("Ctrl+c key presses");)));

Butt, how can you listen to the next Ctrl + Enter keys:

$ (Document). keydown (function (e) (// with macOS X subtrim if ((e.ctrlKey || e.metaKey) && (e.keyCode == 13 || e.keyCode == 10)) (/ / your dії ...))

Butt, with different names keydown and keyup:

...

jQuery - Submit form elements

In jQuery, you can see step subs for form elements and not only:

  • focus (focusin)
  • blur (focusout)
  • change
  • input (for text elements form)
  • select
  • submit

jQuery - Subtract and give focus

Posing focus overpowers an element if it takes focus away. Given a subtype to be generated for the input, select and send elements (a href = "..."), as well as any other elements that have tabindex authority set. Otrimanya element focus zvіysnyuєtsya after an additional click or pressing the Tab key on the keyboard. Podiya focus does not merge.

Krim focus is also similar to podia, it is called focusin. On the vіdmіnu vіd focus podіya splivaє is given, and it can, for example, vikoristovuvatisya for displaying podії focus batkіvskimi elements.

The blur effect is overpowered by the element if it takes focus. So just like a focus, a blur sub can be similar to a focus out sub. It is given that it looks like blur team, that it can be split. Tse mozhlivіst can vikoristovuvat, for example, for otrimanna yogo on batkіvskih elements, and not only on those elements, like yogo viklikav (target).

For example, when you remove the focus subdiv with the div element, you can set the background to an orange color:

...

In the same way, for the help of focus and blur, don’t see the viconate, so you can’t merge the stench:

$("#Demo input"). focus(function()($(this).parent().css("background-color","orange");)) .blur(function()($(this).parent().css("background -color "," inherit ");));

jQuery - Submit change (change)

The change subfield is used for registering changes in the value of input, textarea and select elements. For select elements, checkboxes, and radio buttons, a vinicating window is given (so that only a short time can be chosen). But for other elements, a podia is given not to be taken until such a feast, until the element has been put into focus.

The buttstock of the switch under the change for the post behind the camp of the checkbox element. The availability of the button will be determined by the status depending on which state (checked number) the ensign should be changed to:

...

An example, as we can see, is how to take the value of the select element when changing it:

...

Butt, as we can see, how to take away all the selected elements of the select when changing it:

...

An example of a programmatic wiki to change for the select element:

// list - element id change $("# list"). Trigger("change"); // short entry $("#list"). Change(); // weeklik only for the summary of the subdivision change $ ( "# list"). TriggerHandler("change");

Butt change to change the value of the input element:

...

Ale krіm podії change є more podіya input for text elements. Given a subtitle for a change view, it is generated immediately, and not after that, as a given element, the focus is applied.

An example, using the input to change the value of the input element:

$("Input"). on("input", function()(var value = $(this).val(); console.log(value);));

An example, in which case one of the ways to take the value of the textarea element is:

...

Butt, as we can see, as for the help of change, take the value of the inverse element of input and type, equal to radio:

Windows linux macOS...

jQuery - Submit select (select)

The select selection subfield is generated by the browser if it's in the middle of input elements with type = "text" or textarea sees text.

$("#Target"). Select (function () (console.log ( "Clicking the form of subselect");));

jQuery - Submit form edits (submit)

Podіya submit vinikaє at the element, if koristuvach is trying to correct the form. The given pod_ya can be added only to the form elements.

Example, vikoristannya submit submit:

...
...

Program wiki to edit the form:

$("#Feedback"). submit(); $("#Feedback"). Trigger("submit");

jQuery - Scroll Pod (scroll)

For the purpose of scrolling in jQuery, the sub-scroll subya is highlighted.

For example, let's look at the function on the scroll side, as if it would render an element with the scrollup class, so that the scroll value is greater than 200px and attach it, so that the scroll value is less than this value.

// function shorthand $ (window).scroll (function () (// dії when scrolling side ... if ($ (this).scrollTop ()> 200) ($ ( ". Scrollup"). FadeIn () ;) else($(".scrollup").fadeOut();)));

jQuery - How to resize the window (resize)

To listen, please change the browser window to resize:

For example, let's create a sample, which, when changing the browser window, will display the width and height in the end of the side:

$(Window).resize(function()($("body").append("

Width x Height = "+ window.innerWidth +" x "+ window.innerHeight +"

"); });

jQuery - Querying Standard Submission Behavior

Some elements in HTML may have standard behavior. For example, if the coristuvach presses on the force, then follow the address specified in the href attribute. If you do not need this, then yoga can be said. To say the standard behavior, you need to call in the callback by following the preventDefault method of the event object.

For example, let's talk about the standard behavior of all messages on the side that the service class can be:

$("a.service"). On ( "click", function (e) (// standard browser default e.preventDefault ();

What is fusion and what is yogo zupiniti

Krіm skasuvannya standard dії, in mekhanіzmі podіy є shche tak understand yak splivannya. It is clear that if the browser generates a pod, then it is not necessary to work only for the flow element (qile), but for all other options, including the father:

Flow element (meta) -> parent element meta -> parent element -> ... -> document -> window

In jQuery, there are scripts, if in the representation of a language a certain element of the same element has a copy for a given subdivision, it is not necessary to select one. And so that the podia does not expand by the whole element, it is necessary to improve it. For which item in the sample it is necessary to call the stopPropagation method of the event object. If the sound of the call to the method of the podia rumbles, and you will not be spitting.

For example, if you move the cursor to an element with the mark class, it would turn orange instead.

Actual text... fragment...... continue ...
...

In this case, if you do not specify the stopPropagation method, then when the cursor is brought to the span element with the mark class, a window is given not only for this, but for all other elements. And tse in this example was created before the color change is not only for the text laid in the span, but for the entire paragraph.

If you need to specify the default behavior of the browser and the merging function, then in jQuery you can simply change the callback of these two methods by turning the result of the function to false.

$("A"). On("click", function(e)(//e.preventDefault(); //e.stopPropagation(); ... return false;));

Addition to dynamically created objects

In order to put a pod on an element, as there is no other way, you can win the following construction of the on function:

$(Document).on(eventName, selector, handler); // document otherwise be some other essential element // eventName - name of the sub // selector - selector, which will filter the fields, for which it is necessary to run the handle of the handle // handler - the handle of the handle

Qiu diyu can zdіysniti zavdyaki to the one who podіya merges, and, later, blames all the ancestors of this element. And the object, to which to merge all subdivisions on the side, is document. That is why, in the majority of vipadkiv, choose yoga itself. If you know the selector, the on function can programmatically select the middle elements (elements that call the target (target) and all of the ancestors including the father) tі, yak yoma. And let's see for all the selected elements the number of assignments in the function on the sampler. Dії, for the help of some kind of processing, move to another element (ancestor), called in jQuery another delegation process.

For example, dodamo approach to the element, which is not seen on the side:

Delegation can be zastosovuvatisya not only for processing subdivisions of dynamically created objects, but in order not to tie them to the skin element (yakscho їx on the side can be even richer) a sampler.

For example, in the comments go to zvnіshnіm possilannyam(Such ones will be redirected to the away side):

$(Document).on("click","#comment a",function(e)(if(!(Location.hostname === this.hostname ||!this.hostname.length))(e.preventDefault( ); location.href = "away? link =" + encodeURIComponent($(this).attr("href"));)));

jQuery - Pod Visualizer

Vidalennya obroblyuvachіv podії zdіysnyuєtsya for help method off. With this, for the help of the new one, you can only see those samples, as if adding for the help of the on method.

Call to the off method without arguments in the name of the elements, all add to it the subscripts.

For example, we can turn on all the customizations for elements with the link class:

$(".link"). off();

For example, we can see that the click on all elements with the link class:

$(".link"). off("link");

The special selector (**) allows you to see only the delegated subdivisions were not delegated:

$(".link"). off("click", "**");

Visibility only designated delegate subdivisions (behind an additional selector):

// adding a delegated sub $ ("ul"). On ( "click", "li", function () (// display the content of the li element in the console console.log ($ (this) .text ());)) ; // deleting the delegated sub $ ("ul"). off("click", "li");

View all openModal wrappers of the delegated click sub in the modal namespace for show class elements:

$("Body"). on("click.modal", ".show", openModal);

Creation of the recognized for koristuvach podії

To create powerful triggers in jQuery, use the on and trigger methods.

The principle of creating a special sub jQuery can be seen behind the help of an attacking butt:

...

At the last topics, the undertones were seen, which are blamed at the onslaught of keys. In these topics, we can look at other subforms. At that number, form the administration on the server.

Focus element

If the shape element is in focus, the focus podium is called, and if the shape element is out of focus, the blur podium is called. We create an input field and display the text in the console when the focus is removed and when the focus is exited:

HTML code:

There is no sense in vikoristannі tsikh podіy to change the style of the element. For which, in CSS, use the focus pseudo-class.

input value

input input Vinicate if there is another text in the input field. It will be used when adding or removing a skin symbol, as well as when copying the text in the input field. In some browsers, it works silently: when a skin character is entered, the blur sub-dialog is created, after which the input sub-dialog is created, and then the focus sub-subject is created. If there is no way to get focus and blur, then there is no problem in that. But if the stench is, then the stench will be spratsovuvat when the skin is added to the symbol.

Add one more tag to the side and we will show its value when entering the text:

HTML code:

JavaScript:

28
29
30
31
32

var newv = document.getElementById("new_value"); newv.onchange = function () (console.log("Value changed to:" + newv.value););

Form management

Podiya submit spratsovuє when vіdpravtsi forms on the server. Vono vikoristovuєtsya for the re-verification of data, yakі koristuvach vvіv in the form. In the sampler, the values ​​​​of the elements of the form are corrected, and if the values ​​\u200b\u200bare entered incorrectly, then try to correct the form. When you call, you should be reminded, in which it is indicated that it is necessary to correct.

Let's create tags into the form and add the button to edit the form:

HTML code:

In the front butts, the elements of the form were already found, so there is no need to repeat the survey in the collection.

If the form is filled in correctly, so that all fields need two characters, then the browser will try to run the file, specifying in the action attribute, and wink pardon, because there is no such file. Ale, in whom there is no risk, it is not necessary for him to be afraid to change the script to the robot. If less than two symbols are entered in one of the fields, then an update will appear and the correction will be applied. If you want to do a lot of reworking, then you can for a skin rechecking bring your own maintenance and improve the strength of the form.

The input event fires when the value of an element is used to create interactive controls for web-based forms in order to accept data from the user; a wide variety of types of input data and control widgets are available, depending on the device and user agent. "> , Element represents a control that provides a menu of options ">