简体   繁体   中英

Jquery Function running multiple times

I am using two scripts on my page and there is a general click function which records the number of clicks one user is making. So when I click on any element in the document, the click function should run after which other functions on the same element runs. But in my case, the click function runs multiple times before passing the control to the other function.

/************ 1st Jquery Script ***************/
    function($) {
      $(function(e) {
       $('.signupCustom').click(function(){
        var email = $('#form-email').val()
        var password = $('#pass').val()
        var firstName= $('#form-first-name').val()
        var lastName= $('#form-last-name').val()
        var number= $('#form-mobile').val()
        var type=$('#sel1').val()

        $.ajax({
            url: '/login',
            type: 'GET',
            data: {
                'email': email,
                'password':password,
                'firstName':firstName,
                'lastName':lastName,
                'number':number,
                'type':type
            },
            success: function(data){
                if ($('#sel1').val() == "Travel-Agent"){
                    window.location.href = "/agentVerification.html"
                }
                else{
                    window.location.href = "/dashboardTraveller"
                }
            }
        })
    })

    $('.login').click(function(){
        var email = $('#form-username').val()
        var password= $('#form-password').val()
        $.ajax({
            url: '/loginCustom',
            type: 'GET',
            data: {
                'email': email,
                'password':password
            },
            success: function(data){
                window.location.href = data['url']
            }
        })
    })

    $('.destinationsButton').click(function(){

        var url="/destinations";
        window.location=url;
    })


});   })(jQuery);

I have attached the link to the html page which contains the second script.

Link to Page

If you go to this link, there is an image:

在此处输入图片说明

When I click on Login button, the click function runs multiple times before control goes to other function. I want the click function to run single time and then control should go to other function.

I tried e.stopPropagation but in case of a popup on same page, the popup does not open. Here on clicking on login, popup comes.

Is there any reason you are using the below?

jQuery('*').on("click",function(e){});

I think this might work better

jQuery('body').on("click",function(e){});

good luck

I don't know where you're stuck but you may use one to run the click function just once:

$(selector).one('click',function(){
   //code runs only one click
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM