Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/425.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery:序列化数组返回空字符串_Javascript_Jquery - Fatal编程技术网

Javascript jQuery:序列化数组返回空字符串

Javascript jQuery:序列化数组返回空字符串,javascript,jquery,Javascript,Jquery,我没有忘记按原样添加name属性,但是我的序列化表单返回一个空字符串。我做错了什么 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(fu

我没有忘记按原样添加name属性,但是我的序列化表单返回一个空字符串。我做错了什么

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>

不知道这是否是一种更好的方法,但您可以为此编写自己的插件,例如:

(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);

演示

非常确定它对图像输入不起作用。这是因为类型=图像。。。请参见rsubmitterTypes=/^?:提交|按钮|图像|重置|文件$/i,然后在中。序列化有!rsubmitterTypes.test类型确定,那么我应该如何修复它?我想要一个图像…不要通过AJAX提交图像数据。这对于一些看起来很简单的东西来说是非常复杂的,但是谢谢!