如何触发我的 jquery 表单提交
How to trigger my jquery form submit
现在表单正在提交,但它使用的是默认网址,我希望它在我的javascript中使用表单提交事件,以便它通过.ajaxSubmit()选项。
这是javascript。
$('#selectedFile').change(function() {
var id = $('#selectedFile').data('id');
var options = {
target: '#output',
beforeSubmit: showRequest,
success: showResponse,
url: '/ManageSpaces/UploadImage',
data: { id: id },
type: 'post'
};
$("#imageform").trigger('submit'); // I want this to trigger the submit below! Not the default form submit.
$('#imageform').submit(function () {
$(this).ajaxSubmit(options);
return false;
});
});
<form id="imageform" enctype="multipart/form-data">
<input type="file" id="selectedFile" style="display: none;" data-id='@Model.YogaSpaceId' />
<input type="button" value="Add Photos" class="btn" id="pictureupload" />
</form>
操作方法
[HttpPost]
public ActionResult UploadImage(int id, HttpPostedFileBase image)
{
//do some stuff to save the image
return Json("Saved");
}
您在注册提交处理程序之前触发提交,在您的情况下,一种选择是直接在更改处理程序中调用ajaxSubmit
,而无需使用提交处理程序,如下所示。
$('#selectedFile').change(function () {
var id = $('#selectedFile').data('id');
var options = {
target: '#output',
beforeSubmit: showRequest,
success: showResponse,
url: '/ManageSpaces/UploadImage',
data: {
id: id
},
type: 'post'
};
$('#imageform').ajaxSubmit(options);
});
另一种选择是在更改处理程序之外注册提交处理程序,如下所示,并在更改处理程序中触发提交
$('#selectedFile').change(function () {
$("#imageform").trigger('submit'); // I want this to trigger the submit below! Not the default form submit.
});
$('#imageform').submit(function () {
var id = $('#selectedFile').data('id');
var options = {
target: '#output',
beforeSubmit: showRequest,
success: showResponse,
url: '/ManageSpaces/UploadImage',
data: {
id: id
},
type: 'post'
};
$(this).ajaxSubmit(options);
return false;
});
相关文章:
- AngularJS JQuery Ajax表单提交等效
- 当所有输入文本字段都为空时,禁止表单提交,但当jquery中的任何字段不为空时允许提交
- setTimeOut AFTER jQuery表单提交
- jquery表单提交无法使用多个输入(IE和Firefox)
- Jquery表单提交仍在刷新
- 如何在没有表单提交、没有js、没有jquery和没有ajax的情况下将输入的文本框值存储到php变量中
- 如何触发我的 jquery 表单提交
- 表单提交在一段时间循环PHP,jquery
- 在提交之前验证表单,jQuery发布到javaScript,然后提交到确认页面
- 'Alajax' Jquery 简单表单提交.如果表单未经验证,如何防止提交
- 使用jquery进度条重定向到表单提交后的另一个页面
- 表单提交onChange事件;不能使用JQuery函数
- jQuery在表单提交和重定向后发出警报
- Jquery:如何处理"停止/取消”;用户停止表单提交时的事件
- 我想知道如何只激活表单提交,而不获取jquery中ajax url的值
- Jquery、AJAX&PHP,表单提交不起作用
- 在初始表单提交后使用ajax/jQuery自动刷新,然后更改页面标题
- 如果电子邮件输入处于焦点上,则阻止输入按钮表单提交 (jquery)
- 防止hubspot表单提交- jQuery验证
- 如何防止表单提交 JQuery