附加带有onclick事件的部分事件 [英] Append a partial with onclick event

查看:123
本文介绍了附加带有onclick事件的部分事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的onclick事件,但涉及部分显示时:

仅显示文本:

<%= escape_javascript(render(:partial => payment)) %>

我希望它获取parial并显示

这是我的代码:

$(".payment").append("<%= escape_javascript(render(:partial => payment)) %>"); 

在付款时尝试使用"和"

完整的jquery代码

$(document).ready ->
  $('.plans').change ->
    $(".payment").append("<%= escape_javascript(render(:partial => 'payment')) %>");

如果我以后使用devise,以后再有人看这个的话,这对我有用:

  devise_scope :user do
    get 'registrations/toggle_partial' => "registrations#toggle_partial"
  end

将toggle_partial.js文件放入

views/devise/registrations

尼克·M放在那里的其他东西都很棒....

还要记住部分名称周围的''

解决方案

基于上面的完整jquery"代码,我猜想您在coffeescript文件中拥有此文件,该文件无法访问render方法.

您将必须设置一条路由,该路由会触发呈现JS的控制器动作,如下所示:

routes.rb:

get 'controller_name/toggle_partial' => "controller_name#toggle_partial"

在该控制器中:

def toggle_partial
  respond_to do |format|
  format.js
end

然后在views/controller_name中添加一个名为toggle_partial.js的文件,其内容如下:

$(".payment").append("<%= escape_javascript(render(:partial => payment)) %>");

然后在coffeescript文件中执行以下操作:

$('.plans').change ->
  $.ajax(
    type: 'GET'
    url: '/controller_name/toggle_partial'
    success: ( data, status, xhr ) ->
  )

很抱歉,如果在Coffeescript示例中缩进不可用.最重要的是,您无权在coffeescript文件中进行渲染,因此您必须进行变通. 希望这会有所帮助.

I have my onclick event working but when it comes to displaying the partial it:

only displays the text:

<%= escape_javascript(render(:partial => payment)) %>

I want it to fetch the parial and display it

here is my code:

$(".payment").append("<%= escape_javascript(render(:partial => payment)) %>"); 

tried with "" and '' around payment

full jquery code for this

$(document).ready ->
  $('.plans').change ->
    $(".payment").append("<%= escape_javascript(render(:partial => 'payment')) %>");

edit:

this worked for me if anyone looks at this in the future since i use devise:

  devise_scope :user do
    get 'registrations/toggle_partial' => "registrations#toggle_partial"
  end

put the toggle_partial.js file in

views/devise/registrations

everything else that NickM put there is great....

also remember the '' around the partial name

解决方案

Based on the 'full jquery' code you have above I'm guessing you have this in a coffeescript file, which does not have access to the render method.

You're going to have to set up a route that hits a controller action that renders the JS, like this:

routes.rb:

get 'controller_name/toggle_partial' => "controller_name#toggle_partial"

In that controller:

def toggle_partial
  respond_to do |format|
  format.js
end

And in views/controller_name add a file called toggle_partial.js with these contents:

$(".payment").append("<%= escape_javascript(render(:partial => payment)) %>");

Then in your coffeescript file do something like this:

$('.plans').change ->
  $.ajax(
    type: 'GET'
    url: '/controller_name/toggle_partial'
    success: ( data, status, xhr ) ->
  )

Sorry if the indentation is off on the coffeescript example. The bottom line is that you don't have access to render in the coffeescript file so you have to make a work-around. Hope this helps.

这篇关于附加带有onclick事件的部分事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆