Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
248 views
in Technique[技术] by (71.8m points)

JQuery, Ajax: Serialize() not working with <select> in IE?

I did create a little script for form select filtering. My goal is, that when the user selects something in let's say "field1", then "field2" get's replaced by a filtered one with PHP with only the selection left (done via data in an array).

My JQuery works fine in almost every modern browser:

$(".ajx").on("change", function() {
    var $form = $('form');
    var $fields = $(".ajx");
    var method = $form.attr("method").toUpperCase();
    var $this = $(this);
    //var $ser = $fields.serialize();
    var $ser = $this.serialize();

    console.log($ser);
    //console.log($(this))

    $('.ajx_load').show();

    $.ajax({
        url: $form.attr("action"),
        data: $ser,
        type: method,
        success: function(res) {
            //$('.ajx').html($(res).find('.ajx div'));
            //$this.html($(res).find('.ajx div'));

            var $fnd = '#' + $this.attr('id') + ' div';
            $this.html($(res).find($fnd));

            console.log($this.attr('id'));
            console.log($this.html($(res).find($fnd)));

            $('.ajx_load').hide();
        },
        error: function (xhr, status) {  
            $('.ajx_load').hide();
            alert('Unknown error - status: ' + status); 
        }
    });
});

The code should reload the html code in <fieldset class="axj" id="ajx_1"><div>, keep the selection made on the field and replace all <select> fields in this <fieldset> by filtered ones via PHP.

Here is the HTML output (without PHP) used: http://jsfiddle.net/69rvLtcn/8/

The serialize() function does not work in IE (tested in IE11) and Safari - it is empty!

Thanks in Advance for your help

EDIT: I tried to set a setTimeout() in the "success: function". When I do this (even when I set it to 1) the same thing that happens in IE/Safari then happens on every other Browser too...?

It's like the selection is not transmitted in the JQuery response?!

EDIT2: It's IE after all! It somehow doesn't serialize request...? If I in PHP I check the request with var_dump($_REQUEST); in IE/Safari it's empty in all others not...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Got the solution:

JQuery:

The .serialize() method should be called on a form or on a set of inputs.

Source: http://bugs.jquery.com/ticket/9863


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...