Why is this form not submitting inside the target

2019-06-07 22:34发布

问题:

I've created a form that targets an iframe to submit an image. I absolutely had this working previously, but now the whole page submits, instead of just the iframe. I'm at a total loss.

HTML:

<form id="upload-form" name="upload-form" class="" action="/handle/upload" method="post" enctype="multipart/form-data">
    <div class="fileinput-wrap">
        <label for="fileinput">Image input</label>
        <input type="file" name="file" id="fileinput" />
     </div>
    <input type="submit" id="submitter" name="submitter" />
</form>

JS:

$("#fileinput").on('change', function () {
    var $iframe = $("<iframe />").attr({
        id: 'frame_uploader',
        name: 'frame_uploader'
    });
    var $img = $("<img />");
    var imageUrl = "";
    $("#upload-form").prepend($img).append($iframe)
        .attr('target', 'frame_uploader')
        .trigger('submit');
    $iframe.load(function () {
        var imageUrl = $iframe.contents().find("body").text();
        $img.attr('src', imageUrl);
    });
});

http://jsfiddle.net/NVp9K/

回答1:

I read somewhere that certain iframe names are reserved. Changing the name of the iframe has fixed the problem.



回答2:

Maybe you should add frame id and name attribute when it is created. Just like this:

var $iframe = $("<iframe id=\"frame_uploader\" name=\"frame_uploader\" />");