【问题标题】:How to know whether PartialView is loaded by JQuery?如何知道 PartialView 是否被 JQuery 加载?
【发布时间】:2015-08-25 22:46:56
【问题描述】:

我的目标很简单——我想在下一个空的 div 中添加一张图片。 例如,我有三个 div。如果第一个 div 不为空,我想给第二个 div 添加一张图片。

所以,我有一个非常简单的看法:

<div id="imageDivF"></>
<div id="imageDivSecond"></>
<div id="imageDivT"></>

一个非常简单的局部视图:

<img id="hello"/>

将部分视图加载到 div 中的 AJAX 代码:

$(document).ready(function () {
        datatype:'json',
        url: '/Home/UploadFiles',
        autoupload: true,
        done: function (e, data) {
   if ($("#imageDivF").has("img").length==0) 
{ $('#imageDivF').load('/Home/ImagePartialView', { address: data.result.name });
// in this next row I would like to check whether #imageDivF contains image
//I should check whether #imageDivF contains image to decide whether it is necessary to put image to the next empty div
//pseudo code: if(#imageDivF) does not contain image, then image should be inserted to the next div #imageDivSecond
    }

如何检查“#imageDivF”是否包含局部视图(内部图像)? 我试过的:

$('#imageDivF').find('#hello');//not detected
 $(''hello", "'#imageDivF")// not detected
 $('#imageDivF #hello')// not detected

【问题讨论】:

    标签: javascript jquery ajax asp.net-mvc asp.net-mvc-4


    【解决方案1】:

    .load(url, data, cb)方法中有回调函数:

    $('#imageDivF').load('/Home/ImagePartialView', { address: data.result.name }, function(){
        alert($(this).find('img').length !== 0); // true if image is there.
    });
    

    您必须在回调函数中检查这一点,因为.load() 具有async 自然进程,因此它不会保留代码执行。每当.load() 方法执行时,下一行也将被执行,因为它不等待.load() 完成。

    这就是为什么存在回调函数的原因,您可以将代码执行放入其中。

    【讨论】:

    • 我需要 'if' 语句。因为我应该检查#imageDivF是否包含图像来决定是否需要将图像放入下一个空div: //伪代码:如果(#imageDivF)不包含图像,则应该将图像插入下一个div #imageDivSecond .有可能吗?
    • 我的编码方式是否正确检查第一个 div 是否有图像?
    • @StepUp 绝对可以检查 if 条件,如果 div 为空,则可以检查该特定元素,然后调用 .load() 方法。
    • 如何实现!?请查看我的更新代码
    • 我不知道如何使用'if'运算符和回调方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-05
    • 2012-02-16
    • 1970-01-01
    • 1970-01-01
    • 2016-09-06
    • 2013-01-12
    • 1970-01-01
    相关资源
    最近更新 更多