Ajax成功功能问题

My javascript looks like that.

There are some problems that I can't figure out, where I did mistake

  • #status_message doesn't show generated message
  • $("#status").className = 'fail'; doesn't override current class

enter image description here

var message=$("#status_message"), form=$("#bcscan"), ids=$('#itemids'),proctype, destination, counter=0, tenWordCounter = 0, autoPostInterval=null, errorcount=0, successcount=0;

function ajaxPost() {
    formData = form.serialize()+'&process=Scan';    
    formUrl = form.attr('action');
    formMethod = form.attr('method');

    $.ajax({
        url: formUrl,
        type: formMethod,
        dataType: "json",
        data: formData,
        success: function (data) { 
            var now = new Date();
            if(data.err_detected==="yes")
            {     
                if(data.errors.indexOf(",") != -1)
                    errorcount=errorcount+data.errors.split(",").length;
                else errorcount=errorcount+1;                
                $("#status").className = 'fail';
                message =errorcount+" errors found";
                $('#errors').prepend(data.errors).slideDown("slow");                        
            }  
            $('#success').prepend(data.success).slideDown("slow");
            if(data.success.indexOf(",") != -1)
                successcount=successcount+data.errors.split(",").length;
            else successcount=successcount+1;              
            message +="Last submitted:"+now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds();
            $("#status_message").text(message); 
        }
    });   
}

Here is page in action

If you want to test, please choose some option from selects like, output from db, ebay.fr and press "scan". Enter 10 digits seperated by comma try 41,42 (they exist in db tables) too between them. After 10th digit it will post textarea via ajax.

For the message, you've declared a jQuery object pointing at the element:

var message = $('#status_message'), ...

but then you're overwriting it with a string:

message = errorcount + ' errors found';

You should be calling:

message.text('some string...')

to change its contents.

For the class change, the correct syntax is:

$("#status").addClass('fail');

did you try this?? $("#status").addClass( 'fail');

How about

$('#status').attr('class','fail');

This will override the current class.