R
R
Rinat Sadykov2017-05-06 19:51:52
JavaScript
Rinat Sadykov, 2017-05-06 19:51:52

Ajax handler works for all forms on the page. How to make an exception for one form?

I have 3 forms on my site. Successfully processed and sent to a common handler. It is necessary that the first form works through its ajax handler.

$(document).ready(function () {
    $("form").submit(function () {
        // Получение ID формы
        var formID = $(this).attr('id');
        // Добавление решётки к имени ID
        var formNm = $('#' + formID);
        $.ajax({ 
            type: "POST",
            url: 'mail.php',
            data: formNm.serialize(),
            success: function (data) {
                // Вывод текста результата отправки
                $(formNm).html(data); 
            },
            error: function (jqXHR, text, error) {
                // Вывод текста ошибки отправки
                $(formNm).html(error);         
            }
        });
        return false;
    });
});

As I understand it, the script looks for all form tags and gets their ID into a variable.
How to make my first form on the site work on a different handler. Tried adding:
var formIDfirst = $(this).attr('id');
        if ((formIDfirst != 'main-form')) {

did not help. I really ask for help. Thanks

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stalker_RED, 2017-05-06
@Ruinevo

The easiest way is to change the selector so that the handler is hung up only on the second two.
Add a class to them, for example, and instead write Or, without adding the class: , but this is worse in terms of support and understanding of what is happening on the page in general. example: https://jsfiddle.net/L39uaxth/$("form").submit(...)$(".myClass").submit(...)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question