当前位置: 动力学知识库 > 问答 > 编程问答 >

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 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..

分享给朋友:
您可能感兴趣的文章:
随机阅读: