Question

I'm stuck on strange behavior: for one of the forms $().serialize() method doesn't work (returns empty string). Only in Opera (on Linux, 12.16). Works fine in Chromium, FF and Opera on Android.

Form is

<form id="new-story-form" role="form" >
    <div class="form-group">
        <label for="txt-storyname" class="control-label">Story name</label>
        <input type="text" name="name" class="form-control" id="txt-storyname" required="required">
    </div>
    <div class="form-group">
        <label for="num-storylength" class="control-label">Story length</label>
        <input type="number" name="length" class="form-control" id="num-storylength" value="20" min="10" max="500" required="required">
    </div>
    <div class="form-group">
        <button type="submit" class="btn btn-default">Начать</button>
    </div>
</form>

Javascript for submitting is:

        $("#new-story-form").submit(function (e) {
            e.preventDefault();
            var post_data = $(this).serialize();
            console.log(post_data);

            $.post("/post", post_data ,function (data) {
                console.log(data);
                show_message("S", 'success');
                var update = setTimeout(function () {
                    location.reload();
                }, 1000);

            }).fail(function (err) {
                        $.each(err.responseJSON, function (index, value) {
                            console.log(index, value);
                            show_message(index + ' - ' + value, 'danger');
                        });

                    });
        });

if I look to var post_data = $(this).serialize(); in the Opera debugger, $(this) contains normal form object, but post_data is "".

Is it something that I can miss here?

Était-ce utile?

La solution

The issue seems to be the name "length" of the input, choose another name.

I guess it's a conflict with the built-in property "length" of a form.

There is a note in the jQuery-documentation: Forms and their child elements should not use input names or ids that conflict with properties of a form, such as submit, length, or method. Name conflicts can cause confusing failures.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top