如何在 form_remote_tag 的 :complete 回调中获取表单标签的 JS DOM/JQuery 对象?
在 Rails 2.3.3 中,我想做类似的事情:
<% form_remote_tag :url => "/some/path/", :loading => "loadingFunction(this)", :complete => "completeFunction(this,request)" do %>
基本上,我希望能够将该特定表单标记传递给 :complete 或 :loading 回调函数(以相对方式 - 我不想使用 ID - 我希望能够获得 :complete 调用对应的标签,作为 :complete 调用针对表单标签**的自然结果。
因此,“this”、“jQuery(this)”、“jQuery(this).closest( '.someParentElementOfTheForm')" 等不起作用。
**我这样做是因为: 当页面加载时,我渲染任意数量的“form_remote_tag”,并且用户可以触发导致任意更多“form_remote_tag”出现的事件(每个表单中的内容相似,但每个表单中的元素数量不同)形式也是任意的)。如果我在整个页面中有一个 form_remote_tag,则存在几个主要问题:
用户将仅编辑页面的一个部分(在我当前的设计中对应于一个表单),但所有表单元素'页面中的数据必须在请求中发送
It 某种混乱的方式来跟踪表单及其元素,给它们 ID,例如:“form-12”、“form-12-paramfield-13”等。
这使得很难允许:用户提交一个表单,虽然这种形式通过 AJAX 加载,提交其他表单(包括触发导致新表单出现的事件然后提交该表单的可能性),因为跟踪每个请求的响应数据的位置存在问题
In Rails 2.3.3, I want to do something like:
<% form_remote_tag :url => "/some/path/", :loading => "loadingFunction(this)", :complete => "completeFunction(this,request)" do %>
Basically, I want to be able to pass that specific form tag to the :complete or :loading callback functions (in a RELATIVE way - I don't want to use an ID - I want to be able to get the tag that the :complete call corresponds to as a natural result of the :complete call being for the form tag**).
See Rails documentation for form_remote_tag. In particular, Rails generates code like the following for the onsubmit event: new Ajax.Request('/some/path/', {asynchronous:true, evalScripts:true, onComplete:function(request){completeFunction(this,request)}, onLoading:function(request){loadingFunction(this)}, parameters:Form.serialize(this)}); return false;" . Note how the function calls are wrapped within function(request){} which is inside new Ajax.Request().
So, "this", "jQuery(this)", "jQuery(this).closest('.someParentElementOfTheForm')" etc. don't work.
**I am doing this because:
I'm rendering an arbitrary number of "form_remote_tag"s with when the page is loaded, and the user can trigger events that cause arbitrarily more "form_remote_tag"s to appear (with similar content in each form, but the amount of elements in each form is also arbitrary). If I have one form_remote_tag in the entire page, there are a few main problems:
The user will only be editing one section of the page (which in my current design corresponds to one form), but all of the form elements' data in the page will have to be sent in the request
It would require some sort of messy way to keep track of forms and their elements, giving them IDs such as: 'form-12','form-12-paramfield-13' etc.
It makes it very difficult to allow: the user to submit one form, and while that form is loading via AJAX, submit other forms (including the possibility of triggering an event that causes a new form to appear and then submitting that form), because of issues with keeping track of where to put the response data for each request
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过在 html 选项中传递以下回调,例如
:html => {:加载=> “#{远程函数(选项)};返回 false;” }
或者
:html => { : 加载 => “#{远程函数(选项)};返回 false;” 您
可以指定的回调有(按顺序):
可以通过为特定状态代码添加其他回调来进一步细化 :success 和 :failure。
最近,我已经实现了在我的远程表单中覆盖 onsubmit,如下所示
Have you tried the by passing below callbacks in html options like
:html => { :loading => "#{remotefunction(options)}; return false;" }
or
:html => { :onloading => "#{remotefunction(options)}; return false;" }
The callbacks that may be specified are (in order):
You can further refine :success and :failure by adding additional callbacks for specific status codes.
Recently I have achieved to overiride onsubmit in one my remote forms as below