N
N
nano_e_t_42015-10-25 16:06:10
JavaScript
nano_e_t_4, 2015-10-25 16:06:10

How to hang two scripts on one div?

Hello everyone
Guys, plz tell me how you can solve this problem:
There is a button on the webcam, by clicking on it using ajax, the value of this button is transmitted to the server. The script on the server makes a selection and returns this selection to the webcam, where it is inserted into the submenu. The problem is that each time the button is clicked, a selection procedure will occur.
Question: is it possible to somehow "optimize" the process so that on the first click the transfer script is executed, and on the next n clicks it is just a drop-down menu with already received values?
(most likely a crazy idea, but the whole world is full of madness :) )

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
Maxim Vostrugin, 2015-10-25
@nano_e_t_4

You can check the menu for submenus. If not, then we make a request.
$(".submenu ul li").length > 0

V
Vitaly Inchin ☢, 2015-10-25
@In4in

We put the handler on the click in which we make the request and delete the current handler. We put a new one after processing the request.

°• The simplest
button.onclick = function(){
   delete button.onclick;
   var xhr = new XMLHttpRequest();
   ....
   xhr.onload = function(){
         //Что-то делаем
         button.onclick = function(){
             //Выпадание меню
         }
   }
}

°•If there are other handlers on the button
button.addEventListener("click", function one(){
   button.removeEventListener("click", one);
   var xhr = new XMLHttpRequest();
   ....
   xhr.onload = function(){
         //Что-то делаем
         button.addEventListener("click", function(){
             //Выпадание меню
         });
   }
});

°• Same with jQuery
$(button).one("click", function(){
   $.ajax(...).done(function(){
         //Что-то делаем
         $(button).click(function(){
             //Выпадание меню
         });
   });
});

S
Schoolboy., 2015-10-25
@viphorizon

.append() ? I did not understand the question...

A
Andrey Malinovsky, 2015-10-25
@GAVRAN

If I understood you correctly, something like this:

var resultReturned = true;
$("#button").click(function(){
   if(resultReturned===false){
     return;
   }
   $.ajax({
     url: "test.html",
     beforeSend: function() {
        resultReturned = false;
     }
    }).always(function() {
        resultReturned = true;
        //обрабатываем результат
    });
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question