最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to stop function from running in javascript, jquery? - Stack Overflow

programmeradmin2浏览0评论

I have the following function:

function checkEmails(newEmail){
    $('table td:nth-child(3)').each(function(){
        if ($(this).html() == newEmail)
        {
            alert('The email address "' + newEmail + '" is already in the list.  Duplicates are not allowed.');
            toggleSpinner();
            return false;
        }           
    });
    return true;
} 

I'm calling it this way in my form submit handler:

if (!checkEmails($('input#email', $('#newForm')).val())) {
  return false;
}//I submit the form via ajax next....

I'm just checking to make sure that the email address the user's trying to submit isn't already in a table. It seems to work good, except in Firefox, it doesn't actually stop the ajax request from occurring. The alert box appears, telling me the user's already in the list, but after clicking ok, the form is submitted anyway. It works as I want it to in IE.

What am I doing wrong here?

I have the following function:

function checkEmails(newEmail){
    $('table td:nth-child(3)').each(function(){
        if ($(this).html() == newEmail)
        {
            alert('The email address "' + newEmail + '" is already in the list.  Duplicates are not allowed.');
            toggleSpinner();
            return false;
        }           
    });
    return true;
} 

I'm calling it this way in my form submit handler:

if (!checkEmails($('input#email', $('#newForm')).val())) {
  return false;
}//I submit the form via ajax next....

I'm just checking to make sure that the email address the user's trying to submit isn't already in a table. It seems to work good, except in Firefox, it doesn't actually stop the ajax request from occurring. The alert box appears, telling me the user's already in the list, but after clicking ok, the form is submitted anyway. It works as I want it to in IE.

What am I doing wrong here?

Share Improve this question asked Jan 21, 2010 at 21:24 croceldoncroceldon 4,61512 gold badges60 silver badges97 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

it should probably be done like this:

function checkEmails(newEmail){
    var ret = true;
    $('table td:nth-child(3)').each(function(){
        if ($(this).html() == newEmail)
        {
            alert('The email address "' + newEmail + '" is already in the list.  Duplicates are not allowed.');
            toggleSpinner();
            ret = false;
        }           
    });
    return ret;
} 

What it is doing is setting the return value to true before doing the each on the elements to, then if it finds any invalid email addresses it will set it to false. That is the value that will be returned from the function.

the return false is inside the closure so it doesn't break out of the outer function

i.e. it returns false for the nested function and not for checkEmails

I think you want this (use a bigFatGlobal to store the return value):

function checkEmails(newEmail){
    var bigFatGlobal = true;

    $('table td:nth-child(3)').each(function(){
        if ($(this).html() == newEmail)
        {
            alert('The email address "' + newEmail + '" is already in the list.  Duplicates are not allowed.');
            toggleSpinner();
            bigFatGlobal = false;
        }           
    });
    return bigFatGlobal;
}
发布评论

评论列表(0)

  1. 暂无评论