2011-12-01 40 views
3

我有一个表单,它在提交时附加一个加载程序映像。表单的响应显示在页面上的iFrame上。我希望一旦iframe获得了必要的内容,在一定的延迟时间之后淡出装载器映像。检查iFrame的内容并使用jquery回复

我很新的jQuery,所以我对代码的粗略理解表示歉意,我不确定什么是错误的或正确的与下面的代码。任何帮助都感激不尽。

感谢

代码在这里:

$(function() { 

    $(".submitButton").click(function() { 

     $('#mainDiv').append('<img src="images/ajax-loader.gif" class="loaderIcon" id="loaderIcon" alt="Loading..." />'); 

    }); 

    if($('#iFrameName').contents().val()!==""){  

     delay(3000); 
     $('#mainDiv img.loaderIcon').fadeOut(1000); 
    } 
}); 

回答

1
//bind an event handler to the `load` event for the iframe 
$('#iFrameID').on('load', function() { 

    //get the contents of the `body` element in the iframe 
    var response = $(this).contents().find('body').html(); 

    //here you can do what you want with the response variable, for instance you can check for the existance of a specific element 
    if (response.filter('#successID').length > 0) { 

     //if there is an element in the iframe with the id of `successID` then the loaderIcon will be removed 
     $('#loaderIcon').remove(); 
    } 
}); 

注:.on()是新的一样的jQuery 1.7的,如果你正在使用jQuery的旧版本那么我建议使用.bind()代替(语法在这个例子中是相同的)。

+0

非常感谢,已经工作一种享受!非常感激 – Um21

0

在父页面,添加:

function hideLoader() { 
    $('#loaderIcon').remove(); 
} 

如果您的Iframe反应地说:

<script type='text/javascript'> 
parent.hideLoader() 
</script>