-1

I did not forget to add name attributes as is a common problem and yet my serialized form is returning an empty string. What am I doing wrong?

HTML/javascript:

<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script>
$( document ).ready( function() {
    $('#word_form').submit(function(e) {
        e.preventDefault();
        console.log($(this).serialize()); //returns an empty string
    });
});
</script>
</head>

<body>
    <div id="wrapper">
        <form name="word_form" id="word_form" method="POST">
            <input type="image" name="thumbsUp" id="thumb1" value="1" src="http://upload.wikimedia.org/wikipedia/commons/8/87/Symbol_thumbs_up.svg" style="width:50px;height:50px;">
            <input type="image" name="thumbsDown" id="thumb2" value="2" src="http://upload.wikimedia.org/wikipedia/commons/8/84/Symbol_thumbs_down.svg" style="width:50px;height:50px;">
        </form>
    </div>
</body>
4
  • Pretty sure it won't work for image inputs. Commented Jun 10, 2014 at 3:24
  • 1
    it is because of the type="image"... see rsubmitterTypes = /^(?:submit|button|image|reset|file)$/i then in .serialize() there is !rsubmitterTypes.test( type ) Commented Jun 10, 2014 at 3:27
  • Ok so how should I fix it? I want an image... Commented Jun 10, 2014 at 3:32
  • Don't submit image data over AJAX. Commented Jun 10, 2014 at 3:45

1 Answer 1

3

dont know if this is a better way, but you could write your own plugin for this, something like:

(function($) {

    $.fn.serializeAll = function() {

        var toReturn    = [];
        var els         = $(this).find(':input').get();
            console.log("Elements:" + els);
        $.each(els, function() {
            if (this.name && !this.disabled && (this.checked || /select|textarea/i.test(this.nodeName) || /text|hidden|password/i.test(this.type) || this.src)) {
                var val = $(this).val();
                toReturn.push( encodeURIComponent(this.name) + "=" + encodeURIComponent( val ) );
            }
        });

        return toReturn.join("&").replace(/%20/g, "+");    
    }

})(jQuery);
//and use it
var serialized = $('#word_form').serializeAll();
console.log(serialized);

Demo jsFiddle

Sign up to request clarification or add additional context in comments.

1 Comment

This is stupidly complicated for something that seems so simple but thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.