[jQuery] form提交到iframe之后,获取iframe里面内容

时间:2022-04-17 15:55:35

过程不多说,直接上代码

<script type="text/javascript" src="js/jquery-1.8.0.min.js"></script>
<script type="text/javascript">
var iframe_id_publish = 'works-publish-iframe';
jQuery(function(){
    jQuery('.works-publish-form').submit(function(){
        var $_this = jQuery(this);
        $_this.attr('action', 'test.php');

        //TODO 根据实际情况添加验证规则
        return verify_works_publish_form();
    });

    iframe = document.getElementById(iframe_id_publish);
    if (iframe.attachEvent){
    //兼容IE
        iframe.attachEvent("onload", function(){
            iframe_loaded();
        });
    } else {
    // ff, chrome
        iframe.onload = function(){
            iframe_loaded();
        };
    }
});

function iframe_loaded() {
    //这里就读到form提交到iframe,iframe加载完成之后的页面
    var iframe_obj = jQuery(document.getElementById(iframe_id_publish).contentWindow.document.body);
}

/**
 * 验证表单 
 * 
 * @access public
 * @return void
 */
function verify_works_publish_form() {
    return true;
}
</script>
<form class="works-publish-form" target="works-publish-iframe" method="post" action="">
    <input type="text" name="title" value="" />
    <input type="submit" />
</form>
<iframe name="works-publish-iframe" id="works-publish-iframe" frameborder="0" width="490" height="92" style="display:none;"></iframe>

 

Have fun with jQuery!