如何在 form_remote_tag 的 :complete 回调中获取表单标签的 JS DOM/JQuery 对象?

发布于 2024-11-25 02:33:46 字数 1358 浏览 1 评论 0原文

在 Rails 2.3.3 中,我想做类似的事情:

<% form_remote_tag :url => "/some/path/", :loading => "loadingFunction(this)", :complete => "completeFunction(this,request)" do %>

基本上,我希望能够将该特定表单标记传递给 :complete 或 :loading 回调函数(以相对方式 - 我不想使用 ID - 我希望能够获得 :complete 调用对应的标签,作为 :complete 调用针对表单标签**的自然结果。

请参阅 form_remote_tag 的 Rails 文档。特别是,Rails 为 onsubmit 事件生成如下代码: new Ajax.Request('/some/path/', {asynchronous:true, evalScripts:true, onComplete:function(request){completeFunction(this,request)} , onLoading:function(request){loadingFunction(this)}, 参数:Form.serialize(this)}); return false;" 。注意函数调用是如何包装在 new Ajax.Request() 内的 function(request){} 中的。

因此,“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼眸里的快感 2024-12-02 02:33:46

您是否尝试过在 html 选项中传递以下回调,例如

:html => {:加载=> “#{远程函数(选项)};返回​​ false;” }
或者
:html => { : 加载 => “#{远程函数(选项)};返回​​ false;” 您

可以指定的回调有(按顺序):

:loading:   Called when the remote document is being loaded with data by the browser.
:loaded:    Called when the browser has finished loading the remote document.
:interactive:   Called when the user can interact with the remote document, even though it has not finished loading.
:success:   Called when the XMLHttpRequest is completed, and the HTTP status code is in the 2XX range.
:failure:   Called when the XMLHttpRequest is completed, and the HTTP status code is not in the 2XX range.
:complete:  Called when the XMLHttpRequest is complete (fires after success/failure if they are present).

可以通过为特定状态代码添加其他回调来进一步细化 :success 和 :failure。

最近,我已经实现了在我的远程表单中覆盖 onsubmit,如下所示

 <% options = {:url => {:action =>"test", :id => 1}, :with =>"Form.serialize('test_form')" }%>
 <% form_remote_tag :html=>{ :onsubmit =>"#{remote_function(options)}; return false" } do %>

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):

:loading:   Called when the remote document is being loaded with data by the browser.
:loaded:    Called when the browser has finished loading the remote document.
:interactive:   Called when the user can interact with the remote document, even though it has not finished loading.
:success:   Called when the XMLHttpRequest is completed, and the HTTP status code is in the 2XX range.
:failure:   Called when the XMLHttpRequest is completed, and the HTTP status code is not in the 2XX range.
:complete:  Called when the XMLHttpRequest is complete (fires after success/failure if they are present).

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

 <% options = {:url => {:action =>"test", :id => 1}, :with =>"Form.serialize('test_form')" }%>
 <% form_remote_tag :html=>{ :onsubmit =>"#{remote_function(options)}; return false" } do %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文