gpt4 book ai didi

javascript - 自动点击/强制触发js功能

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

我在我的 MVC View 中..

 //Submit file
<% using (Html.BeginForm("MethodName","ControllerName", FormMethod.Post, new { enctype = "multipart/form-data"})) { %>
<input type="file" name="file" id="file"/>
<input type="text" id="file-name" name="Id"/>
<input type="submit" value="Submit" name="Submit"/>
<% } %>

//Save link.
<span class="Update" onclick="js.function()">Update</span>

单击“提交”按钮后,我会转到 Controller 和运行一些将文件提交到数据库的代码的方法。完成后,我需要自动点击/强制点击保存链接,以便在提交后运行 js 函数。我该怎么做。

最佳答案

我会使用 Ajax.BeginForm,在 OnSuccess 处理程序中您可以单击按钮。您的代码看起来像这样(未经测试但应该会引导您朝着正确的方向前进):

<% using (Ajax.BeginForm("MethodName","ControllerName",
new AjaxOptions {
OnSuccess ="OnSuccess",
OnFailure ="OnFailure"
},
new {
enctype = "multipart/form-data"
})) { %>
<input type="file" name="file" id="file"/>
<input type="text" id="file-name" name="Id"/>
<input type="submit" value="Submit" name="Submit"/>
<% } %>

Javascript:

function OnSuccess() {
$('.Update').trigger('click');
}

编辑:如果您没有尝试上传文件,我上面的答案就是您想要的。但是,当我注意到您正在尝试发布文件时,我对此进行了更多研究;如果不使用 html 5、iframe hacks 等,则无法使用 ajax 上传文件。在我看来,最好的选择是使用这样的 uploader uploadifyplupload根据您的设置方式,使用 html5、flash 或 hack 解决此问题。

此外,binding-httppostedfilebase-using-ajax-beginform是一个类似的问题,可能会有帮助。

关于javascript - 自动点击/强制触发js功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11955247/

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