简体   繁体   中英

Unable to find error in ajax function submitting form

I have this function where in IE8 only the alert doesn't fire and the ajax part is not run at all. The form is then submitted despite the return false . Why is this happening? Why is it only happening in IE8?

$(document).ready(function(){
    $('form[name="DownloadForm"]').submit(function(){
        var valCases = new Array()
        valCases.push(($('input[name="firstname"]').val()!="") ? true : false )
        valCases.push(($('input[name="lastname"]').val()!="") ? true : false )
        valCases.push(($('input[name="company"]').val()!="") ? true : false )
        valCases.push(($('input[name="email"]').val()!="") ? true : false )

        alert(valCases.indexOf(false)) //this alert doesn't fire!


        if (valCases.indexOf(false)==-1) {
            $.ajax({
                url: url,
                type: 'POST',
                data: $(this).serialize(),
                beforeSend: function(){
                    //before
                },
                success: function(data) {
                    //do something
                },
                error: function(data) {
                    //error
                }
            });
            return false;
      } else {
            $('.inl-table tr').each(function(index){
                if (index==valCases.indexOf(false)) {
                    $(this).next().children('th').css('color','red');
                }
            });
            return false;
      }
      return false;
    });

});

The problem might be, that this array function doesn't exist in IE8:

valCases.indexOf(false)

That won't explain the missing alert, of course :)
EDIT: Ok, I read your code wrong. It explains the missing alert ;)

You'll find a polyfill under this link: MDN - Array indexOf

Since you're already using jQuery, you might aswell use jQuery.inArray which falls back to a native indexOf function when available.

use out the ajax function it:

var dd =$(this).serialize();

in ajax

data :dd,

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