gpt4 book ai didi

javascript - 对象存在,对象改变值 JS、JQuery

转载 作者:行者123 更新时间:2023-11-30 06:33:11 25 4
gpt4 key购买 nike

当我的应用程序启动时,这个 JSonResult 不存在,而我想使用它时(我收到错误“TypeError”)

var newImg = $.parseJSON($("#UploadTarget").contents().find("#jsonResult")[0].innerHTML);

我想做什么?我希望有事件告诉我此对象存在或此对象更改值。

 var newImg = $.parseJSON($("#UploadTarget").contents().find("#jsonResult")[0].innerHTML);

这个应该怎么写?

using System.Web.Mvc;

namespace pol
{
public class WrappedJsonResult : JsonResult
{
public override void ExecuteResult(ControllerContext context)
{
context.HttpContext.Response.Write("<html><body><textarea id=\"jsonResult\" name=\"jsonResult\">");
base.ExecuteResult(context);
context.HttpContext.Response.Write("</textarea></body></html>");
context.HttpContext.Response.ContentType = "text/html";
}
}
}

当我点击按钮图像上传到服务器但结果从未打开重要方法 UploadImage_Complete();

@model po.Models.Stwna
@using (Html.BeginForm("UploadImage", "StronaGlowna", FormMethod.Post,
new
{
enctype = "multipart/form-data",
id = "ImgForm",
name = "ImgForm",
target = "UploadTarget"
}))
{
<input type="file" name="imageFile" />

<input type="button" class="button" value="@Model.ZO" onclick="UploadImage()" />
}
<iframe id="UploadTarget" name="UploadTarget" onload="UploadImage_Complete();" style="position: absolute;
left: -999em; top: -999em;"></iframe>
<div id="Images">
</div>
<script type="text/javascript">
var isFirstLoad = true;

function UploadImage() {
$("#ImgForm").submit();
}
function UploadImage_Complete() {
//Check to see if this is the first load of the iFrame
if (isFirstLoad == true) {
isFirstLoad = false;
return;
}

//Reset the image form so the file won't get uploaded again
document.getElementById("ImgForm").reset();

//Grab the content of the textarea we named jsonResult . This shold be loaded into
//the hidden iFrame.
var newImg = $.parseJSON($("#UploadTarget").contents().find("#jsonResult")[0].innerHTML);

//If there was an error, display it to the user
if (newImg.IsValid == false) {
alert(newImg.Message);
return;
}



//Create a new image and insert it into the Images div. Just to be fancy,
//we're going to use a "FadeIn" effect from jQuery
var imgDiv = document.getElementById("Images");
var img = new Image();
img.src = newImg.ImagePath;

//Hide the image before adding to the DOM
$(img).hide();
imgDiv.appendChild(img);
//Now fade the image in
$(img).fadeIn(500, null);


// $('#Images').text(newImg.ImagePath);
}
</script>

MVC

[HttpPost]
public WrappedJsonResult UploadImage(HttpPostedFileWrapper imageFile)
{

return new WrappedJsonResult
{
Data = new
{
IsValid = true,
Message = string.Empty,
ImagePath = Url.Content(String.Format("http://a1.ec-images.myspacecdn.com/images01/29/4982945eb42646efafe2f94855ac3d21/l.jpg"))

}
};
}

当我点击按钮两次时,我得到了这个图像,但是当我点击一次时,我没有得到任何结果。

最佳答案

我决定使用 Timer 来制作这个 Simple Timer

关于javascript - 对象存在,对象改变值 JS、JQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16257172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com