2013-01-15 55 views
2

我需要在动态生成的字符串中的iframe中显示图像。 iframe的ID是upload_target使用jquery在iframe中加载图像

这是我使用

// assign esrc and upath 
$.ajax 
({ type: "POST", 
    url: "editImage.php", 
    data: {esrc: editsource, upath: upath } , 
    success: function(esource) 
    { //load into iframe 
     s = esource; 
     console.log(s); 
     $('#upload_target').contents().find('body').html(s); 
    } 
}); 

文件editImage.php简单地返回正确的文件名中的代码。控制台显示正确的文件名,所以我知道那里没有问题。但是iframe没有任何事情发生,它只是保持空白。我能够用另一个事件填充相同的iframe,即提交一个表格,其target就是这个iframe。但为什么它不在这里工作?

我也试过$('#upload_target').attr('src',s),但是我得到了一个406错误。

回答

0

最后,我只是砍死一个疯狂的修复。我不知道这是否合理,但它的工作原理。 而不是使用ajax,我只是创建了一个隐藏输入的表单,它保存了我需要传递给editImage.php并提交该表单的值。然后iframe加载图像。

$('#imgdialog').append("<form id='imageeditform' method='post' action='editImage.php' target='upload_target'><input type='hidden' name='esrc' value='"+editsource+"' /><input type='hidden' name='upath' value='"+$('#upath').val()+"' /></form>"); 
$('#imageeditform').submit(); 
3

尝试

...find("body").append($("<img/>").attr("src", s).attr("title", "sometitle")); 

我没有调试代码,但应该工作。

编辑

此代码工作正常,我

<iframe id="upload_target"><html><body></body></html></iframe> 

<script type="text/javascript"> 
    $(function() { 
     $('#upload_target').contents().find('body').append($("<img/>").attr("src", "http://www.bing.com/az/hprichbg/rb/NYBirds_ROW10420117938_1366x768.jpg").attr("title", "sometitle")); 
    }); 
</script> 
+0

我试着追加,但没有区别。 – user961627