Skip to content Skip to sidebar Skip to footer

Why Is Return Function Different After Click

In following code $('body').delegate('.submit',... passed = search && passed; ... after click on button in form i have in var passed output as true in case var result in fu

Solution 1:

Try changing this line:

passed = search && passed;

To:

passed = search() && passed;

Edit

After careful consideration of what you seemed to want your code to do, I rewrote the logic of it to this:

var searchResult = false;
var search = function(e) {
    e.preventDefault();
    $.ajax({
        type: 'POST',
        url: 'myURL',
        data: myData,
        async: false,
        success: function(data) {
            if (data == '0') {
                searchResult = false;
            } else {
                searchResult = true;
            }
        }
    });
};

$('.myclass_1').live('click', search);
$('.myclass_2').live('keyup change', search);

$('body').delegate('.submit', 'click', function(e) {
    var passed = true;

    if (!(searchResult && passed)) { // <-- NOTICE THIS
        $('#loadingDiv, #overlay').hide();
        return false;
    }
});

Setting a global variable searchResult and modifying that in the search() function means you don't have to return anything from it. You can now check searchResult right in the submit button's click event. You're attaching the search() function as the callback to .myclass_1's click event and .myclass_2's keyup and change events, so now the search() function will fire and properly set searchResult based on the ajax response.


Solution 2:

There are a few issues here.

First, your method search sets an additional .live handler on keyup/etc., which I don't understand. If your goal is to run search on those events, don't bind the events in search again.

Second, what is the search variable in the delegate function? Is that supposed to represent the return value of the search() function, or is it something else? If it's supposed to be search()'s return value, it's not--in order for it to be that you should be setting a global (or namespaced global) as discussed in your previous question.

I'd recommend hitting a few JavaScript and jQuery tutorials before going too much further; it will save some time in the long run. Throwing quantities of code at the problem without having a handle on why/how it works is ultimately counterproductive.


Solution 3:

 passed = search && passed;// this line is missing something

it always returns true because you are not testing the return value of search but rather whether it's defined on that scope (in which it is)

try this

   $('body').delegate('.submit', 'submit', function (event) /* USE the event obj to pass it to search() */ 
      {
       var passed = true;
       //passed = required_selectbox() && passed;
       //passed = required_rediuses() && passed;
       passed = search(event) && passed;// CHANGES ARE HERE
       alert(passed); // This output is always 'true' !!!!!!!!!!!!!?
       if (!passed) {
           $('#loadingDiv, #overlay').hide();
           return false;
       }
   });

Post a Comment for "Why Is Return Function Different After Click"