开发者

jQuery - only fires on second click when changing iframe src

I'm trying to change the src of an iframe upon submitting a form.

The iframe src won't change unless I hit the submit button twice. However, if I just change the input 'type' for my submit button to 'text', it works on the first click - but obviously doesn't开发者_如何学C submit the form.

<script>

$(document).ready(function() { 
    $("#form1").submit(function() {
            $('#upload_frame').attr('src','upload-test.php');
    });
});

</script>

<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
  <input id="file" type="file" name="file" />
  <input name="Submit" id="submit" type="submit" value="Submit" />
<br />
  <iframe id="upload_frame" name="upload_frame"> </iframe>
</form>


I figured out the issue I was having. Resolved it with the setTimeout function.

    //show iframe on form submit
        $("#form1").submit(function(){

            if (show_bar === 1) { 
                $('#upload_frame').show();
                function set () {
$('#upload_frame').attr('src','upload_frame.php?up_id=<?php echo $up_id; ?>');
                }
                setTimeout(set);
            }
        });
    //

The project in which I'm using this form can be found here:

http://www.johnboy.com/php-upload-progress-bar


You can change the type to button and do this...

$(document).ready(function() { 
    $("#submit").click(function() {
        $('#upload_frame').attr('src','upload-test.php');
        $("#form1").submit();
    });
});


Why don't you use the target attribute of the form ?

<form ... target="upload_frame">

should do it ..

And you would not need to resort to jQuery for standard and supported functionality of the browser..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜