通过 ajax 将 jQuery 插件 (jquery-autosave) 用于 MVC 3 控制器方法
我的 javascript/ajax 经验不足可能会阻止我弥补这里的差距。我已经实现了一个名为 jQuery-autosave 的 jQuery 插件。我的表单控件上的更改事件触发 jquery.autosave.js 文件中的方法,但是,它没有达到“成功”。我相信这是因为我无法弄清楚 jquery.autosave 方法正在寻找什么。
我看到很多帖子的主题可能与此相关,但是,我无法将这些点联系起来。
所以这就是我正在寻找的帮助...我的视图中的脚本需要什么才能使用此插件通过控制器中的方法(saveRespose)持久保存表单中每个输入控件的值?而且,如果问得不多的话,如果你能解释一下你是如何确定脚本需要什么的,这样我就可以更多地了解它是如何工作的,那就是锦上添花了?
jquery-autosave 插件的代码可以在上面的链接中找到,所以我不会在这里发布。作为这个主题的新手,我可能需要提供更多内容。请向我解释一下您需要什么。
我的视图脚本在这里...
<script type="text/javascript">
jQuery(function ($) {
$("form").autosave({
callbacks: {
trigger: ["change", function () {
var self = this;
$("[name=save]").click(function () {
self.autosave();
});
} ],
save: {
method: "ajax",
options: {
whatever: "This is it",
url: "/Profile/saveResponse/"
, success: function () {
alert("saved!");
}
}
}
}
});
});
</script>
谢谢, 布莱恩
My javascript/ajax inexperience is probably preventing me from bridging a gap here. I have implemented a jQuery plugin called jQuery-autosave. The change event on my form control fires the method in the jquery.autosave.js file, however, it does not hit "success". I believe this is because I cannot figure what the jquery.autosave method is looking for.
I see many postings where the subjects could be related to this one, however, I have not been able to connect the dots.
So this is the help I'm looking for... What does the script in my View need to use this plug-in to persist the value of each input control in my form via a method (saveRespose) in my controller? And, if it is not to much to ask, the frosting on the cake would be if you could explain how you determined what the script needed so that I can learn a little more about how this works?
The code for the jquery-autosave plugin can be found at the link above so I won't post here. Being new to this subject matter, I'll probably need to provide more. Please explain to me what you need.
My View script is here...
<script type="text/javascript">
jQuery(function ($) {
$("form").autosave({
callbacks: {
trigger: ["change", function () {
var self = this;
$("[name=save]").click(function () {
self.autosave();
});
} ],
save: {
method: "ajax",
options: {
whatever: "This is it",
url: "/Profile/saveResponse/"
, success: function () {
alert("saved!");
}
}
}
}
});
});
</script>
Thanks,
Brian
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该插件的 AJAX 功能与 jQuery.ajax 函数使用的功能相同(也就是说,您可以传递给该函数的任何内容都可以传递给插件的“ajax”方法)。也就是说,通常您会将方法名称传递到 AJAX 请求的“data”参数中,以便让您的控制器相应地处理它。这看起来像:
{ method: 'methodName' }
,jQuery 本质上会变成这样的请求:yourUrl.asp?method=methodName
(假设您使用的是 GET 请求)。实际上,该插件只是帮助您从表单获取数据,以便您可以将其传递给其他东西,例如 AJAX。
The AJAX functionalities of the plugin are identical to those used by the jQuery.ajax function (meaning, anything you can pass into that function can be passed into the 'ajax' method of the plugin). That said, generally you would pass a method name into the 'data' parameter of the AJAX request to let your controller handle it accordingly. This would look something like:
{ method: 'methodName' }
which jQuery essentially turns into a request like:yourUrl.asp?method=methodName
(assuming you're using a GET request).Really, the plugin is just there to help you get the data from the form so you can pass it on to something else, like AJAX.