从 jquery get 请求运行 Rails RJS 模板

发布于 2024-07-21 05:35:16 字数 1132 浏览 5 评论 0原文

我使用 Rails 和 jquery 以及 RJS 模板来执行各种 AJAX 请求。

对于我的大多数 Ajax 内容,我将提交处理程序附加到 application.js 中的表单,如下所示:

$('#tagging_flickr_photos').submitWithAjax();
$('#tag_submit').click(function() {
    $('#flickr-photos-status').show();

});

这会调用表单操作,该操作会进行一些处理,然后转发到 RJS 模板,如下所示:

$("#flickr-photos-status").hide();
$("#flickr-photos").fadeIn();
$("#flickr-photos").html("<%= escape_javascript(render(:partial => 'flickr_photos_for_tagging_content')) %>");

这很有效。

现在我尝试做同样的事情,但只是基于在下拉列表中选择不同的值而不提交表单。 这是我将处理程序附加到下拉列表的 javascript:

$('#film_film_name_id').change(function() {
    $.get('/admin_film/make_tags?film_name_id=' + $("#film_film_name_id").val() + '&film_speed_id=' + $("#film_film_speed_id").val());
});

我的控制器方法执行一些处理,然后转发到 RJS 模板 (make_tags.js.erb):

$("#film_tags").val(<%=@tags%>)

但是,该模板似乎并未执行。 我可以在日志中看到它正在调用我的方法并呈现模板的条目,但无论我在模板中放入什么,似乎都没有发生。 我在那里放置了一个 Javascript 警报,但它没有触发。

我认为问题与附加我的 Javascript 处理程序有关,但我无法弄清楚我缺少什么。

预先感谢您的任何帮助。

I am using Rails and jquery with RJS templates to perform various AJAX requests.

For most of my Ajax stuff I attach a submit handler to the form in my application.js as follows:

$('#tagging_flickr_photos').submitWithAjax();
$('#tag_submit').click(function() {
    $('#flickr-photos-status').show();

});

This calls the form action which does some processing and then forwards to a RJS template as follows:

$("#flickr-photos-status").hide();
$("#flickr-photos").fadeIn();
$("#flickr-photos").html("<%= escape_javascript(render(:partial => 'flickr_photos_for_tagging_content')) %>");

This works a treat.

Now I am trying to do the same but just based on selecting a different value in a dropdown and not submitting a form.
Here's my javascript to attach the handler to the dropdown:

$('#film_film_name_id').change(function() {
    $.get('/admin_film/make_tags?film_name_id=' + $("#film_film_name_id").val() + '&film_speed_id=' + $("#film_film_speed_id").val());
});

My controller method does some processing then forwards to the RJS template (make_tags.js.erb):

$("#film_tags").val(<%=@tags%>)

However, the template doesn't appear to execute.
I can see entries in my logs that it's calling my method and rendering the template but no matter what I put in the template nothing seems to happen.
I've put a Javascript alert in there and it doesn't fire.

I assume the problem is to do with attaching my Javascript handler but I can't figure out what I am missing.

Thanks in advance for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

残疾 2024-07-28 05:35:16

我认为你必须告诉 jQuery 你正在等待 JavaScript 并且它应该被执行。 请尝试以下操作并查看它是否有效:

$('#film_film_name_id').change(function() {
  var url = '/admin_film/make_tags?film_name_id=' + $("#film_film_name_id").val() + '&film_speed_id=' + $("#film_film_speed_id").val()
  $.ajax({
    type: 'GET',
    dataType: 'script',
    url: url
  });
});

请检查 jQuery.ajax() 上的 jQuery 文档 如果它不起作用,因为我还没有测试代码。

I think you have to tell jQuery that you are expecting JavaScript and that it should be executed. Try the following and see if it works:

$('#film_film_name_id').change(function() {
  var url = '/admin_film/make_tags?film_name_id=' + $("#film_film_name_id").val() + '&film_speed_id=' + $("#film_film_speed_id").val()
  $.ajax({
    type: 'GET',
    dataType: 'script',
    url: url
  });
});

Please check the jQuery docs on jQuery.ajax() if it doesn't work as I haven't tested the code.

梦中的蝴蝶 2024-07-28 05:35:16

嗨,

我正在使用直接通过原型库(不是 jquery)调用的观察者,

        <td><div id="outconditionnals_container">
                <% if [email protected]? then %>
                        <%= collection_select(:OUTconditionnal, :id, @milestone.howto.conditionnals, :id, :name, {:prompt => true}, {"index" => @outconditionnalID.to_s}) %>
                        <%= observe_field("OUTconditionnal_"[email protected]_s+"_id",
                                :url => { :action => :AJAX_selection_change },
                                :with => "'id='+value+'&dir=out&type=conditionnal&milestoneID="[email protected]_s+"'",
                                :on => "changed")%>                         
                <% end %>
            </div>
        </td>

然后我的 rjs 从我的操作中呈现以引用其他一些表单元素。

希望这可以帮助。

HI,

I'm using an observer that calls directly through the prototype library (not jquery)

        <td><div id="outconditionnals_container">
                <% if [email protected]? then %>
                        <%= collection_select(:OUTconditionnal, :id, @milestone.howto.conditionnals, :id, :name, {:prompt => true}, {"index" => @outconditionnalID.to_s}) %>
                        <%= observe_field("OUTconditionnal_"[email protected]_s+"_id",
                                :url => { :action => :AJAX_selection_change },
                                :with => "'id='+value+'&dir=out&type=conditionnal&milestoneID="[email protected]_s+"'",
                                :on => "changed")%>                         
                <% end %>
            </div>
        </td>

Then my rjs is rendered from my action to referesh some other form elements.

Hope this helps.

伤痕我心 2024-07-28 05:35:16

或者,您也可以使用您一直在使用的 $.get
这是你的发型示例:

$('#film_film_name_id').change(function() {
    $.get(
      '/admin_film/make_tags', // or something like '<%= make_tags_admin_film_path %>'
      {
        film_name_id: $("#film_film_name_id").val(),
        film_speed_id: $("#film_film_speed_id").val()
      },
      null, // you may define an additional callback here, if that makes sense
      "script"
    );
});

Alternatively, you could also go with the $.get you've been using.
Here's your example with a hairdo:

$('#film_film_name_id').change(function() {
    $.get(
      '/admin_film/make_tags', // or something like '<%= make_tags_admin_film_path %>'
      {
        film_name_id: $("#film_film_name_id").val(),
        film_speed_id: $("#film_film_speed_id").val()
      },
      null, // you may define an additional callback here, if that makes sense
      "script"
    );
});
怂人 2024-07-28 05:35:16

jQuery 方法 getScript 是执行 ujh 上面推荐的操作的一个很好的简写。

The jQuery method getScript is a nice shorthand for doing what ujh recommended above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文