开发者

Trying to get some jQuery functions to run in order. Is callback the issue?

开发者 https://www.devze.com 2023-04-10 14:32 出处:网络
I\'m trying to do some things in order, and I\'m h开发者_运维问答aving some trouble. When the button with the id #sub_button is clicked,

I'm trying to do some things in order, and I'm h开发者_运维问答aving some trouble.

  1. When the button with the id #sub_button is clicked,
  2. Make sure each element with class ".verify" has it's own object value (see code)...
  3. ... if not, blur that element (will run some other code and create an object for it).
  4. AFTER the above IF check is COMPLETE (now all elements should have an object), THEN run function "isitgood". (The "isitgood" function is running before all elements get their object values, which is done on blur)

    $("#sub_button").click(function() {
    
        $(".verify").each(function(){
    
            objtitle = $(this).attr('id');
    
            if (!myObj[objtitle]) { 
    
            $("#"+objtitle).blur(); // Define anything undefined
    
            }
    
    
        }); // end each
    
    isitgood();
    
    }); // end click function
    
    function isitgood(){
    
        if (myObj.login_id == "ok" && myObj.email == "ok")  {
    
        // submit the form
    
        } else {
    
        // shows error
    
        }
    
    }
    

Also, once I get this executing in the right order, it would be nice to do some sort of .each loop to check if all the object values == "ok" instead of specifying all of them in the function. All of the names of the objects (ie. login_id, email) are the ID attr of any element with class name .verify.


Well, you could do a quick index check in the click callback:

var sub_buttons = $("#sub_button");
sub_buttons.click(function() {

    $(".verify").each(function(index){

        objtitle = $(this).attr('id');

        if (!myObj[objtitle]) { 

        $("#"+objtitle).blur(); // Define anything undefined

        }

        if (index == sub_buttons.length - 1)
            isitgood();
        }

    }); // end each

}); // end click function

This will check if you're on the last element in the jQuery object, and if so, will run the isitgood() function. This way, you make sure that you're finished with the $.each method before executing isitgood()


Javascript is asynchronous. Your isitgood() will always fire while .each is still doing it's thing.

That said from your code it's not clear what you're trying to accomplish. The way you're using .each seems to indicate that you have multiple of the same ID attributes on your tags. That won't work, IDs have to be unique. Also you seem to be mixing jQuery and regular Javascript. Use one or the other. Actually just use jQuery, you'll save yourself time and effort!

If you do have unique ids then you shouldn't need the .each at all. Just check the appropriate ids with your if statement.

Please provide more of your code and i can update this with a better answer. For instance what does your myObj look like? How do elements of it get the value of ok? It doesn't seem to get set within your call to .each().

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号