Rails使用jQuery的observe_field [英] Rails observe_field using jQuery

查看:59
本文介绍了Rails使用jQuery的observe_field的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用不带jRails的jQuery,是否有等效于Rails/Prototypeobserve_field方法的

Is there an equivalent to the Rails/Prototype observe_field method using jQuery without jRails?

我正在使用will_paginate进行按需搜索:

I am doing a search-as-you-type with will_paginate:



<%= observe_field('search', 
                  :frequency => 2, 
                  :update => 'results',
                  :loading => "Element.show('spinner')",
                  :complete => "Element.hide('spinner')", 
                  :url => { :controller => 'foo', :action => 'search' }, 
                  :with => "'search=' + escape(value)") %>

推荐答案

使用jQuery,您需要使用选择器,类似的东西应该可以工作

Using jQuery, you'd need to use a selector, something like this should work

$("#search").bind("blur", function() {
  $("#spinner").show(); // show the spinner
  var form = $(this).parents("form"); // grab the form wrapping the search bar.
  var url = form.attr("action"); // grab the URL from the form's action value.
  var formData = form.serialize(); // grab the data in the form
  $.get(url, formData, function(html) { // perform an AJAX get, the trailing function is what happens on successful get.
    $("#spinner").hide(); // hide the spinner
    $("#results").html(html); // replace the "results" div with the result of action taken
  });
});

回复评论

如果您想对键入内容做出反应,则将第一行替换为

If you want to react to keyup, then replace the first line with

$("#search").bind("keyup", // etc...

这篇关于Rails使用jQuery的observe_field的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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